I have a recursive array with same structure of objects and it contains name property. My requirement is to add new property id along with name in recursive array of objects
below is my sample array
[
{
"children": [
{
"children": [
{
"children": [
{
"children": [],
"name": "ID01",
"type": "Under"
},
{
"children": [],
"name": "ID02",
"type": "Under"
}
],
"name": "httpgateway",
"type": "Gut"
},
{
"children": [
{
"children": [],
"name": "mock1",
"type": "Under"
},
{
"children": [],
"name": "mock2",
"type": "Under"
}
],
"name": "mock",
"type": "Gut"
}
],
"name": "23131",
"type": "SEV"
}
],
"name": "integration",
"type": "DataCenter"
},
{
"children": [
{
"children": [
{
"children": [
{
"children": [],
"name": "data1",
"type": "Under"
},
{
"children": [],
"name": "data12",
"type": "Under"
},
{
"children": [],
"name": "data13",
"type": "Under"
},
{
"children": [],
"name": "data14",
"type": "Under"
}
],
"name": "Gut1",
"type": "Gut"
}
],
"name": "213213",
"type": "SEV"
}
],
"name": "dev",
"type": "dt"
}
]
I need Id property along with name as belo
[
{
"children": [
{
"children": [
{
"children": [
{
"children": [],
"name": "ID01",
"id": "ID01",
"type": "Under"
},
{
"children": [],
"name": "ID02",
"id": "ID02",
"type": "Under"
}
],
"name": "gate",
"id": "gate",
"type": "Gut"
},
{
"children": [
{
"children": [],
"name": "mock1",
"id": "mock1",
"type": "Under"
},
{
"children": [],
"name": "mock2",
"id": "mock2",
"type": "Under"
}
],
"name": "mock",
"name": "id",
"type": "Gut"
}
],
"name": "23131",
"id": "23131",
"type": "SEV"
}
],
"name": "int",
"id": "int",
"type": "dt"
},
{
"children": [
{
"children": [
{
"children": [
{
"children": [],
"name": "data1",
"id": "data1",
"type": "Under"
},
{
"children": [],
"name": "data12",
"id": "data12",
"type": "Under"
}
],
"name": "Gut1",
"id": "Gut1",
"type": "Gut"
}
],
"name": "213213",
"id": "213213",
"type": "SEV"
}
],
"name": "dev",
"id": "dev",
"type": "dt"
}
]
I have written method to update this but its not working as expected
const getTreeItemsFromData = (treeItems) => {
console.log('---------------------------', treeItems)
let finalData = []
return treeItems.map((treeItemData) => {
let children = undefined;
if (treeItemData.children && treeItemData.children.length > 0) {
children = this.getTreeItemsFromData(treeItemData.children);
}
let uniqueId = `${treeItemData.name}${Math.floor(Math.random()*(999-100+1)+100)}`;
finalData.push(treeItemData)
console.log("-- ------------------", treeItemData)
});
};
You jus need a function to accept the array and check if the key children exists and is array, make changes to it and then recursively call if it has children.
const t = [
{
"children": [
{
"children": [
{
"children": [
{
"children": [
],
"name": "data1",
"type": "Under"
},
{
"children": [
],
"name": "data12",
"type": "Under"
},
{
"children": [
],
"name": "data13",
"type": "Under"
},
{
"children": [
],
"name": "data14",
"type": "Under"
}
],
"name": "Gut1",
"type": "Gut"
}
],
"name": "213213",
"type": "SEV"
}
],
"name": "dev",
"type": "dt"
}
];
function addIdRec(arr){
arr.forEach(a => {
if(a.children instanceof Array){
a.id = a.name;
if(a.children.length > 0){
addIdRec(a.children);
}
}
})
}
addIdRec(t)
We can do this with a pretty simple recursion:
const addId = (data) =>
data .map (({name, children, ...rest}) =>
({children: addId(children), name, id: name, ...rest})
)
const data = [{children: [{children: [{children: [{children: [], name: "ID01", type: "Under"}, {children: [], name: "ID02", type: "Under"}], name: "httpgateway", type: "Gut"}, {children: [{children: [], name: "mock1", type: "Under"}, {children: [], name: "mock2", type: "Under"}], name: "mock", type: "Gut"}], name: "23131", type: "SEV"}], name: "integration", type: "DataCenter"}, {children: [{children: [{children: [{children: [], name: "data1", type: "Under"}, {children: [], name: "data12", type: "Under"}, {children: [], name: "data13", type: "Under"}, {children: [], name: "data14", type: "Under"}], name: "Gut1", type: "Gut"}], name: "213213", type: "SEV"}], name: "dev", type: "dt"}]
console .log (addId (data))
.as-console-wrapper {max-height: 100% !important; top: 0}
We simply clone the node, adding an id property to match the name one and recur on the children property.
Related
I want to iterate the tree and need to get the id of all the nodes which has the children in string array. while looping it is just returning me the record but doesn't extract the name of the node.
e.g const result = ['root', 'USER', 'ROLE', 'DASHBOARD', 'BRAND', 'COMPANY'];
{
"id": "root",
"name": "Roles and Permissions",
"children": [
{
"id": "USER",
"name": "USER",
"children": [
{
"id": "1",
"name": "VIEW"
},
{
"id": "2",
"name": "CREATE"
},
{
"id": "3",
"name": "EDIT"
}
]
},
{
"id": "ROLE",
"name": "ROLE",
"children": [
{
"id": "8",
"name": "VIEW"
},
{
"id": "9",
"name": "CREATE"
},
{
"id": "10",
"name": "EDIT"
},
{
"id": "11",
"name": "DELETE"
}
]
},
{
"id": "DASHBOARD",
"name": "DASHBOARD",
"children": [
{
"id": "BRAND",
"name": "BRAND",
"children": [
{
"id": "52",
"name": "VIEW"
},
{
"id": "53",
"name": "CREATE"
},
{
"id": "54",
"name": "EDIT"
},
{
"id": "55",
"name": "DELETE"
}
]
},
{
"id": "COMPANY",
"name": "COMPANY",
"children": [
{
"id": "56",
"name": "VIEW"
},
{
"id": "57",
"name": "CREATE"
},
{
"id": "58",
"name": "EDIT"
},
{
"id": "59",
"name": "DELETE"
}
]
}
]
}
]
}
I tried various looping method to get the list, e.g. but not returning the exact name of the node.
function getParent(nodes) {
if(Array.isArray(nodes.children)) {
return nodes.children.map((node) => getParent(node));
}
return nodes.name;
}
You can store the resp in an array and return that array.
const q = {
"id": "root",
"name": "Roles and Permissions",
"children": [
{
"id": "USER",
"name": "USER",
"children": [
{
"id": "1",
"name": "VIEW"
},
{
"id": "2",
"name": "CREATE"
},
{
"id": "3",
"name": "EDIT"
}
]
},
{
"id": "ROLE",
"name": "ROLE",
"children": [
{
"id": "8",
"name": "VIEW"
},
{
"id": "9",
"name": "CREATE"
},
{
"id": "10",
"name": "EDIT"
},
{
"id": "11",
"name": "DELETE"
}
]
},
{
"id": "DASHBOARD",
"name": "DASHBOARD",
"children": [
{
"id": "BRAND",
"name": "BRAND",
"children": [
{
"id": "52",
"name": "VIEW"
},
{
"id": "53",
"name": "CREATE"
},
{
"id": "54",
"name": "EDIT"
},
{
"id": "55",
"name": "DELETE"
}
]
},
{
"id": "COMPANY",
"name": "COMPANY",
"children": [
{
"id": "56",
"name": "VIEW"
},
{
"id": "57",
"name": "CREATE"
},
{
"id": "58",
"name": "EDIT"
},
{
"id": "59",
"name": "DELETE"
}
]
}
]
}
]
}
let result = []
function r(nodes){
if(Array.isArray(nodes.children)){
result.push(nodes.name);
nodes.children.map((c) => r(c))
return result;
}
return result;
}
console.log(r(q))
You can simply use a recursive function. Here ids is an array. You can initialize it before calling the function. Call this function in your getting IDs method.
const getIdFromNodesWithChild = (node) => {
if (node.children != undefined){
ids.push(node.id)
const children_list = node.children
children_list.forEach( new_child => getIdFromNodesWithChild(new_child))
}}
caller function
const returnIds = (tree) => {
ids = []
getIdFromNodesWithChild(tree)
return (ids)
}
result : ['root', 'USER', 'ROLE', 'DASHBOARD', 'BRAND', 'COMPANY']
I'm trying to map through object values and add text if the key is right on JavaScript. Here is our object:
{
"id": "n27",
"name": "Thomas More",
"className": "level-1",
"children": [
{
"id": "n1",
"name": "Rousseau",
"className": "level-2",
"children": [
{
"id": "n2",
"name": "Machiavelli",
"className": "level-3",
"children": [
{
"id": "n9",
"name": "Edison, Thomas",
"className": "level-4"
}
]
}
]
},
{
"id": "n3",
"name": "Einstein",
"className": "level-2",
"children": [
{
"id": "n10",
"name": "Arf, Cahit",
"className": "level-3",
"children": [
{
"id": "n15",
"name": "Rawls, John",
"className": "level-4"
}
]
},
{
"id": "n12",
"name": "Smith, Adam",
"className": "level-3",
"children": [
{
"id": "n11",
"name": "Kant, Immanuel",
"className": "level-4"
}
]
}
]
},
{
"id": "n60",
"name": "Turing, Alan",
"className": "level-2"
}
]
}
I want to add " YES" to their className's. So new object should look like this:
{
"id": "n27",
"name": "Thomas More",
"className": "level-1 YES",
"children": [
{
"id": "n1",
"name": "Rousseau",
"className": "level-2 YES",
"children": [
{
"id": "n2",
"name": "Machiavelli",
"className": "level-3 YES",
"children": [
{
"id": "n9",
"name": "Edison, Thomas",
"className": "level-4 YES"
}
]
}
]
},
{
"id": "n3",
"name": "Einstein",
"className": "level-2 YES",
"children": [
{
"id": "n10",
"name": "Arf, Cahit",
"className": "level-3 YES",
"children": [
{
"id": "n15",
"name": "Rawls, John",
"className": "level-4 YES"
}
]
},
{
"id": "n12",
"name": "Smith, Adam",
"className": "level-3 YES",
"children": [
{
"id": "n11",
"name": "Kant, Immanuel",
"className": "level-4 YES"
}
]
}
]
},
{
"id": "n60",
"name": "Turing, Alan",
"className": "level-2 YES"
}
]
}
I have tried this, but it adds to all of the keys:
const addToClassName = (datasource, fn) => {
return Object.fromEntries(Object
.entries(datasource, fn)
.map(([k, v]) => [k, v && v.children != undefined && v.children.length > 0 ? addToClassName(v.children, fn) : fn(v)])
);
}
let res = addToClassName(obj, v => v + ' YEP');
How can I do it?
You don't need to use Object.fromEntries(), instead, you make your function return a new object, with a transformed className based on the return value of fn. You can then set the children: key on the object to be a mapped version of all the objects inside of the children array. When mapping, you can pass each child into the recursive call to your addToClassName() function. You can add the children key conditionally to the output object but checking if it exists (with children &&) and then by spreading the result using the spread syntax ...:
const data = { "id": "n27", "name": "Thomas More", "className": "level-1", "children": [ { "id": "n1", "name": "Rousseau", "className": "level-2", "children": [ { "id": "n2", "name": "Machiavelli", "className": "level-3", "children": [ { "id": "n9", "name": "Edison, Thomas", "className": "level-4" } ] } ] }, { "id": "n3", "name": "Einstein", "className": "level-2", "children": [ { "id": "n10", "name": "Arf, Cahit", "className": "level-3", "children": [ { "id": "n15", "name": "Rawls, John", "className": "level-4" } ] }, { "id": "n12", "name": "Smith, Adam", "className": "level-3", "children": [ { "id": "n11", "name": "Kant, Immanuel", "className": "level-4" } ] } ] }, { "id": "n60", "name": "Turing, Alan", "className": "level-2" } ] };
const addToClassName = (obj, fn) => ({
...obj,
className: fn(obj.className),
...(obj.children && {children: obj.children.map(child => addToClassName(child, fn))})
});
console.log(addToClassName(data, v => v + " YES"));
If you can change the current obj object then you can achieve this using recursion as
function addClass(obj) {
obj.className += " YES";
obj.children && obj.children.forEach(addClass);
}
const obj = {
id: "n27",
name: "Thomas More",
className: "level-1",
children: [
{
id: "n1",
name: "Rousseau",
className: "level-2",
children: [
{
id: "n2",
name: "Machiavelli",
className: "level-3",
children: [
{
id: "n9",
name: "Edison, Thomas",
className: "level-4",
},
],
},
],
},
{
id: "n3",
name: "Einstein",
className: "level-2",
children: [
{
id: "n10",
name: "Arf, Cahit",
className: "level-3",
children: [
{
id: "n15",
name: "Rawls, John",
className: "level-4",
},
],
},
{
id: "n12",
name: "Smith, Adam",
className: "level-3",
children: [
{
id: "n11",
name: "Kant, Immanuel",
className: "level-4",
},
],
},
],
},
{
id: "n60",
name: "Turing, Alan",
className: "level-2",
},
],
};
function addClass(obj) {
obj.className += " YES";
obj.children && obj.children.forEach(addClass);
}
addClass(obj);
console.log(obj);
You can use lodash, if don't mind.
const _ = require('lodash');
const data = { "id": "n27", "name": "Thomas More", "className": "level-1", "children": [ { "id": "n1", "name": "Rousseau", "className": "level-2", "children": [ { "id": "n2", "name": "Machiavelli", "className": "level-3", "children": [ { "id": "n9", "name": "Edison, Thomas", "className": "level-4" } ] } ] }, { "id": "n3", "name": "Einstein", "className": "level-2", "children": [ { "id": "n10", "name": "Arf, Cahit", "className": "level-3", "children": [ { "id": "n15", "name": "Rawls, John", "className": "level-4" } ] }, { "id": "n12", "name": "Smith, Adam", "className": "level-3", "children": [ { "id": "n11", "name": "Kant, Immanuel", "className": "level-4" } ] } ] }, { "id": "n60", "name": "Turing, Alan", "className": "level-2" } ] };
const newData= _.cloneDeepWith(data, (val, key) => (
(key === 'className') ? `${val} YES` : _.noop()
));
console.log(newData);
// {
// id: 'n27',
// name: 'Thomas More',
// className: 'level-1 YES',
// ...
// }
This is my first question on stack overflow. I am attempting to map an array that is housed within an object. My question is more regarding the targeting than the actual mapping process itself (I think). The goal of my code is to map an array to this target:
var target = {
"id": 1, //as an int
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters": "all of the batter types as a string",
"ingredients": [],//a copy of all the toppings
"countOfFillings": 0
}
the object in question is:
var bakery = {
"items":
{
"item":
[
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0002",
"type": "donut",
"name": "Raised",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0003",
"type": "donut",
"name": "Old Fashioned",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0004",
"type": "bar",
"name": "Bar",
"ppu": 0.75,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
]
},
"topping":
[
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
],
"fillings":
{
"filling":
[
{ "id": "7001", "name": "None", "addcost": 0 },
{ "id": "7002", "name": "Custard", "addcost": 0.25 },
{ "id": "7003", "name": "Whipped Cream", "addcost": 0.25 }
]
}
},
{
"id": "0005",
"type": "twist",
"name": "Twist",
"ppu": 0.65,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
]
},
"topping":
[
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
]
},
{
"id": "0006",
"type": "filled",
"name": "Filled",
"ppu": 0.75,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
]
},
"topping":
[
{ "id": "5002", "type": "Glazed" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
],
"fillings":
{
"filling":
[
{ "id": "7002", "name": "Custard", "addcost": 0 },
{ "id": "7003", "name": "Whipped Cream", "addcost": 0 },
{ "id": "7004", "name": "Strawberry Jelly", "addcost": 0 },
{ "id": "7005", "name": "Rasberry Jelly", "addcost": 0 }
]
}
}
]
}
}
And this is my attempt at mapping. I'm able to get the array generated but only from the first item in the array as indicated with the [0]. Any feedback or advice would be greatly appreciated!
var exampleTest = bakery.items.item.map(mapCake);
function mapCake (aCake)
{
let newType = `${bakery.items.item[1].type}`
let result = {id: `${bakery.items.item[0].id}`,
type: newType,
name: `${bakery.items.item[0].name}`,
ppu: `${bakery.items.item[0].ppu}`,
batters: [`${bakery.items.item[0].batters.batter}`],
ingredients: `${bakery.items.item[0].topping}`,
countOfFillings: `${bakery.items.item[0].fillings}`.length};
return result
}
Try this:
function mapCake(aCake) {
let newType = `${aCake.type}`;
let result = {
id: `${aCake.id}`,
type: newType,
name: `${aCake.name}`,
ppu: `${aCake.ppu}`,
batters: [`${aCake.batters.batter}`],
ingredients: `${aCake.topping}`,
countOfFillings: `${aCake.fillings}`.length
};
return result;
}
Looks like in your function you're directly indexing a specific bakery item each time.
Your aCake parameter wasn't yet being used, at each iteration, aCake refers to the current item that is being iterated over - so that's what you want to be populating your result values with.
I have a scenario were need to iterate over each children element of the tree structure and modify/add properties or attributes. Each children can have multiple children
var treeStructure = {
"id": 1,
"name": "Grand Parent 1",
"children": [
{
"id": 2,
"children": [
{
"id": 3,
"children": [],
"name": "Child 11",
"properties": [
{
"id": 15,
"run": "fast"
},
{
"id": 16,
"walk": "slow"
}
]
},
{
"id": 4,
"type": "Child",
"children": [],
"name": "Child 12",
"properties": [
{
"id": 17,
"run": "slow"
},
{
"id": 18,
"walk": "medium"
}
]
}
],
"name": "Parent 1",
"properties": [
{
"id": 12,
"run": "slow"
},
{
"id": 13,
"walk": "fast"
}
]
},
{
"id": 5,
"children": [
{
"id": 6,
"children": [],
"name": "Child 21"
}
],
"name": "Parent 2",
"properties": [
{
"id": 21,
"run": "medium"
},
{
"id": 22,
"walk": "fast"
}
]
},
{
"id": 7,
"type": "Parent",
"children": [
{
"id": 8,
"children": [],
"name": "Child 31"
}
],
"name": "Parent 3",
"properties": [
{
"id": 31,
"run": "fast"
},
{
"id": 32,
"walk": "slow"
}
]
}
]
}
iterateTree(treeStructure)
iterateTree (node) {
var self = this;
function recursive(obj) {
if (obj.children && obj.children.length) {
obj.children.forEach(function(val,key){
if(val.hasOwnProperty("children")){
self.modifyProperties(val);
recursive(val.children);
}
})
}
}
var expectedOutput = recursive(node);
console.log(expectedOutput)
}
modifyProperties(nodeData) {
let thingProperties = [];
if (nodeData.properties) {
nodeData.properties.map(function (property) {
let tempObj = {
"id": null,
"actionType": "create",
"run" : property.run
};
thingProperties.push(tempObj);
})
}
return {
"id": nodeData.id,
"name": nodeData.name,
"actionType": "create",
}
}
Expected Output: I should be able to modify "id" as null and add
"actionType" as create for children and parent element as shown below
{
"id": 1,
"name": "Grand Parent 1",
"actionType": "create",
"children": [
{
"id": 2,
"actionType": "create",
"children": [
{
"id": 3,
"children": [],
"name": "Child 11",
"actionType": "create",
"properties": [
{
"id": null,
"run": "fast",
"actionType": "create",
"read": "slow"
},
{
"id": null,
"actionType": "create",
"walk": "slow",
"write": "fast"
}
]
},
{
"id": 4,
"type": "Child",
"children": [],
"name": "Child 12",
"actionType": "create",
"properties": [
{
"id": null,
"actionType": "create",
"run": "slow"
},
{
"id": null,
"actionType": "create",
"walk": "medium"
}
]
}
],
"name": "Parent 1",
"actionType": "create",
"properties": [
{
"id": null,
"actionType": "create",
"run": "slow"
},
{
"id": null,
"actionType": "create",
"walk": "fast"
}
]
},
{
"id": 5,
"children": [
{
"id": null,
"children": [],
"name": "Child 21"
}
],
"name": "Parent 2",
"actionType": "create",
"properties": [
{
"id": null,
"actionType": "create",
"run": "medium"
},
{
"id": null,
"walk": "fast"
}
]
},
{
"id": 7,
"type": "Parent",
"actionType": "create",
"children": [
{
"id": null,
"actionType": "create",
"children": [],
"name": "Child 31"
}
],
"name": "Parent 3",
"properties": [
{
"id": null,
"actionType": "create",
"run": "fast"
},
{
"id": null,
"actionType": "create",
"walk": "slow"
}
]
}
]
}
I am trying to loop through a Json object which would look like this
[
{
"yang_type": "container",
"name": "c1",
"value": "",
"children": [
{
"yang_type": "",
"name": "type",
"value": "Uint32",
"children": []
},
{
"yang_type": "list",
"name": "DNS",
"value": "",
"children": [
{
"name": "type",
"value": "String",
"children": [],
"yang_type": ""
},
{
"yang_type": "leaf",
"name": "ip-address",
"value": "",
"children": [
{
"name": "type",
"value": "string",
"children": [],
"yang_type": ""
}
]
},
{
"yang_type": "leaf",
"name": "Domain",
"value": "",
"children": [
{
"name": "type",
"value": "string",
"children": [],
"yang_type": ""
}
]
}
]
}
]
}
]
I am trying this logic but it doesnt loop through first childs child.
while(m.children.length >= 1) {
if(m.yang_type!='' && m.name!=''){
{$log.error("parent:",m.yang_type,m.name);}
}
if(m.name!='' && m.value!=''){
{$log.error("child:",m.name,m.value);}
}
m = m.children[m.children.length - 1];
}
The above code doesn't loop through all the children. what i am doing wrong ?
You try to loop over the array. Your attempt does not work this way.
You could use a callback for iterating and a take it for the recursive call for childrens.
function loop(a) {
console.log(a.name); // process you data
Array.isArray(a.children) && a.children.forEach(loop); // check and iterate children
}
var data = [{ "yang_type": "container", "name": "c1", "value": "", "children": [{ "yang_type": "", "name": "type", "value": "Uint32", "children": [] }, { "yang_type": "list", "name": "DNS", "value": "", "children": [{ "name": "type", "value": "String", "children": [], "yang_type": "" }, { "yang_type": "leaf", "name": "ip-address", "value": "", "children": [{ "name": "type", "value": "string", "children": [], "yang_type": "" }] }, { "yang_type": "leaf", "name": "Domain", "value": "", "children": [{ "name": "type", "value": "string", "children": [], "yang_type": "" }] }] }] }];
data.forEach(loop);
Edit with indented output.
function loop(level) {
return function (a) {
var i = level, s = '';
while (i--) {
s += ' ';
}
if (level) {
s += '*';
}
a.yang_type ?
console.log(s + a.yang_type + ' ' + a.name) :
console.log(s + a.name + ' ' + a.value);
Array.isArray(a.children) && a.children.forEach(loop(level + 1));
}
}
var data = [{ "yang_type": "container", "name": "c1", "value": "", "children": [{ "yang_type": "", "name": "type", "value": "Uint32", "children": [] }, { "yang_type": "list", "name": "DNS", "value": "", "children": [{ "name": "type", "value": "String", "children": [], "yang_type": "" }, { "yang_type": "leaf", "name": "ip-address", "value": "", "children": [{ "name": "type", "value": "string", "children": [], "yang_type": "" }] }, { "yang_type": "leaf", "name": "Domain", "value": "", "children": [{ "name": "type", "value": "string", "children": [], "yang_type": "" }] }] }] }];
data.forEach(loop(0));