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.