~~~
let data = [
{
name: "1",
status: true,
child: [
{
name: "1",
status: true,
child: [
{
name: "1",
status: true,
child: [
{
name: "1",
status: false,
},
{
name: "2",
status: false,
},
],
},
{
name: "1",
status: true,
},
],
}
]
}
]
function formatArr(arr) {
return arr.map((item) => {
if (item.child && item.child.length > 0) {
let newItem = formatArr(item.child);
let status = item.status;
if (newItem.every((v) => v.status == false)) {
status = false;
} else {
status = true;
}
return {
...item,
status,
child: newItem,
};
}
return item;
});
}
console.log(JSON.stringify(formatArr(data)));
~~~