1 回答
TA贡献1757条经验 获得超7个赞
const nestedObj = [
{ id: 1, parentId: null, taskCode: '12', taskName: 'Parent one', duration: 0, assignee: '', crewCount: 0, startDate: null, endDate: null, dependencies: []},
{ id: 2, parentId: 1, taskCode: '12100', taskName: 'Child one', duration: 0, assignee: '', crewCount: 0, startDate: null, endDate: null, dependencies: []},
{ id: 3, parentId: 2, taskCode: '12200', taskName: 'SubChild one', duration: 0, assignee: '', crewCount: 0, startDate: null, endDate: null, dependencies: []},
{ id: 4, parentId: 3, taskCode: '122001', taskName: 'Sub-dub-Child one', duration: 0, assignee: '', crewCount: 0, startDate: null, endDate: null, dependencies: []},
{ id: 5, parentId: null, taskCode: '13', taskName: 'Parent two', duration: 0, assignee: '', crewCount: 0, startDate: null, endDate: null, dependencies: []}
];
function deepDelete(id) {
const index = nestedObj.findIndex((element) => element.id === id);
if (index === -1) return;
nestedObj.splice(index, 1);
const childElements = nestedObj.filter((element) => element.parentId === id);
for (const element of childElements) {
deepDelete(element.id);
}
}
deepDelete(2);
console.log(nestedObj);
添加回答
举报