I have a 3 level object with a code that iterates for each level, if in case the levels increase I would like to improve it so that it is more dynamic, can recursion be used, or even non-recursion but how could it be optimized?
var obj = [{
"seccion1": {
"level1": {
first: "John",
last: "Doe"
},
"level2": {
first: "juan",
last: "perez"
}
}
}]
obj.forEach(function(item) {
Object.keys(item).forEach(function(key) {
let value = item[key];
Object.keys(value).forEach(function(key2) {
console.log(value[key2].first, value[key2].last);
});
});
});
The result should be:seccion1 level1 john doe level2 juan perez