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 for recursive analysis and print all identifiers on the console so that the result looks like this:
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
This is how far I could get. After that, I could not think of any logic.
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 do not understand what logic should be applied here. Help.
Koder source
share