This object contains nested data
var arr = [{
"children": [{
"children": [{
"children": [],
"Id": 1,
"Name": "A",
"Image": "http://imgUrl"
}],
"Id": 2
"Name": "B",
"Image": "http://imgUrl"
}],
"Id":3,
"Name": "C",
"Image": "http://imgUrl"
}]
The goal is to transform the above structure into this format:
[{
"Name": "C",
"Id": 3,
"Image": "http://imgUrl"
}, {
"Name": "B",
"Id": 2,
"Image": "http://imgUrl"
}, {
"Name": "A",
"Id": 1,
"Image": "http://imgUrl"
}]
Below is the original conversion function:
var newArr = []
function getNestedObj(obj){
if(obj.length){
for ( var i=0; i<obj.length; i++){
var newObj = {};
newObj.Name = obj[i].Name;
newObj.Id = obj[i].Id;
newObj.Image = obj[i].Image;
newArr.push(newObj);
if(obj[i].children.length !=0 ){
getNestedObj(obj[i].children)
}
else {
return newArr;
}
}
}
}
How can I simplify this function?