I need assistance with removing the deleted
status from the children key in a nested array of objects using JavaScript. Currently, when I try to filter out the deleted status, I receive an error stating "cannot return filter of undefined." The goal is to ensure that the children key within the obj
variable only contains items with an Active status.
var obj = [
{id:1, label: "sample", children: [{id: 0, status: "Active", name: "xyz"}, {id: 1, status: "Deleted", name: "abc"}]}
{id:2, label: "example"},
{id:3, label: "details", children: [{id:1, status: "Active", name: "finance"}]}
]
var result = removeDeleted(obj);
function removeDeleted(obj){
if (obj.length > 0) {
var list= obj.map(e => {
e.children = e.children.map(child => {
child.children = child.children.filter(c =>
c['status'] !== "Deleted"
);
return child
});
return e
});
return list;
}
}
Expected Output:
[
{id:1, label: "sample", children: [{id: 0, status: "Active", name: "xyz"}]}
{id:2, label: "example"},
{id:3, label: "details", children: [{id:1, status: "Active", name: "finance"}]}
]