Is there are a way to recursively loop over all the nested properties of a JS/jQuery object?
For example, given this object
var x = {
\'name\': \
var x = {
'name': 'a',
'level': 1,
'children': [{
'name': 'b',
'level' : 2,
'children' : [{
'name': 'c',
'level' : 3,
'children' : [{
}]
}]
}, {
'name': 'b2',
'level' : 2,
'children' : [{
'name': 'c2',
'level' : 3,
'children' : [{
}]
}]
}]
}
var step = x;
do {
if (step instanceof Array){
for (i=0; i < step.length; i++) {
callback(step[i]);
}
}
else {
callback(step);
}
step = step.children != undefined ? step.children : null;
} while (step);
function callback (element) {
console.log(element);
}
as long as the structure does not change, you can go down like this.