Please go through this javascript object:
var obj = [{
id: "A",
children: [{
id: "B",
children: [{
id: "C",
children: [{
id: "D",
children: [{
id: "E",
children: [{
id: "F"
}]
}]
}, {
id: "G",
children: {
id: "H"
}
}]
}, {
id: "I"
}]
}, {
id: "J",
children: [{
id: "K"
}]
}]
}, {
id: "L"
}, {
id: "M",
children: {
id: "N",
children: [{
id: "O"
}]
}
}, {
id: "P"
}];
How to write JavaScript code to recursively parse it and print all the IDs in console so that the output looks like:
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
This is how far I could reach. I couldn't think of any logic after that.
for ( i=0 ; i < obj.length ; i++ ){
var objId = obj[i];
for( j=i; j<1 ; j++){
console.log(obj[j].id);
console.log(obj[j].children[j].id);
}
}
I don't understand what logic should be applied here. Do help.
You could use an iterative and recursive approach with a depth-first search algorithm.
Edit: Extended for children as object.
var data = [{ id: "A", children: [{ id: "B", children: [{ id: "C", children: [{ id: "D", children: [{ id: "E", children: [{ id: "F" }] }] }, { id: "G", children: { id: "H" } }] }, { id: "I" }] }, { id: "J", children: [{ id: "K" }] }] }, { id: "L" }, { id: "M", children: { id: "N", children: [{ id: "O" }] } }, { id: "P" }]; data.forEach(function iter(a) { console.log(a.id); if (Array.isArray(a.children)) { a.children.forEach(iter); return; } if (a.children && typeof a.children === 'object') { // omit this part iter(a.children); // if children is } // always an array });
This version collects all necessary data and returns it in an array.
var data = [{ id: "A", children: [{ id: "B", children: [{ id: "C", children: [{ id: "D", children: [{ id: "E", children: [{ id: "F" }] }] }, { id: "G", children: { id: "H" } }] }, { id: "I" }] }, { id: "J", children: [{ id: "K" }] }] }, { id: "L" }, { id: "M", children: { id: "N", children: [{ id: "O" }] } }, { id: "P" }], result = data.reduce(function iter(r, o) { r.push(o.id); if (Array.isArray(o.children)) { return o.children.reduce(iter, r); } if (o.children && typeof o.children === 'object') { // omit this part return iter(r, o.children); // if children is } // always an array return r; }, []); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You could use the below ES6 function. Note that at two places you did not define children
as an array, which I assume is a mistake. If it is indented, I would strongly advise to reconsider, and make it consistent throughout.
function getIds(data) { return data.reduce((acc, el) => acc.concat(el.id, getIds(el.children || [])), []) } var obj = [{ id: "A", children: [{ id: "B", children: [{ id: "C", children: [{ id: "D", children: [{ id: "E", children: [{ id: "F" }] }] }, { id: "G", children: [{ id: "H" }] }] }, { id: "I" }] }, { id: "J", children: [{ id: "K" }] }] }, { id: "L" }, { id: "M", children: [{ id: "N", children: [{ id: "O" }] }] }, { id: "P" }]; console.log(getIds(obj).join('\\n'));
.as-console-wrapper { max-height: 100% !important; top: 0; }
What you are showing looks like it is asking for a depth first solution, as the order of the ids are clearly alphabetical and they are ordered by first encountered then by depth.
As a result, every id encountered will be collected, and then the deeper ids will be examined. This can, and probably should, be done with recursion.
Here is an example.
var obj=[{id:"A",children:[{id:"B",children:[{id:"C",children:[{id:"D",children:[{id:"E",children:[{id:"F"}]}]},{id:"G",children:{id:"H"}}]},{id:"I"}]},{id:"J",children:[{id:"K"}]}]},{id:"L"},{id:"M",children:{id:"N",children:[{id:"O"}]}},{id:"P"}]; var ids = [];//output holder (function tree(cur){ //recursive function for(var i = 0; i < cur.length; i++){ //iterate current set ids.push(cur[i].id); //always store id if(cur[i].children) tree(cur[i].children); //recurse if children exist } })(obj) //start from the top console.log(ids);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.