fetch('...').then((res)=>{
return res.json();
}).then(function(res){
var json=res.response;
for(var i=0; i<json.length; i++){
console.log(json[i])
if(json[i].children!=undefined&&json[i].children.length>0){
for(var j=0; j<json[i].children.length; j++){
console.log(json[i].children[j]);
if(json[i].children[j].children!=undefined&&json[i].children[j].children.length>0){
for(var k=0; k<json[i].children[j].children.length; k++){
console.log(json[i].children[j].children[k]);
if(json[i].children[j].children[k].children!=undefined&&json[i].children[j].children[k].children.length>0){
for(var l=0; l<json[i].children[j].children[k].children.length; l++){
console.log(json[i].children[j].children[k].children[l]);
}
}
}
}
}
}
}
});
const getNestedData = arr =>
Array.isArray(arr)
? arr.flatMap(n => [ n, ...getNestedData(n.children) ])
: [];
// или
function* getNestedData(arr) {
if (arr instanceof Array) {
for (const n of arr) {
yield n;
yield* getNestedData(n.children);
}
}
}
// или
const getNestedData = function*(arr) {
for (const stack = this(arr); stack.length;) {
const n = stack.pop();
yield n;
stack.push(...this(n.children));
}
}.bind(x => x?.constructor === Array ? [...x].reverse() : []);
for (const n of getNestedData(DATA)) {
console.log(n);
}