1 回答

TA貢獻1872條經驗 獲得超4個贊
您可以為此使用遞歸。
var tInput = [{
"id": 0,
"name": "E00-E90 Stoffwechselst?rungen",
"parentId": null,
"children": [{
"id": 1,
"name": "E70-E90 Stoffwechselst?rungen",
"parentId": 0,
"children": [{
"id": 2,
"name": "E70.- St?rungen des Stoffwechsels aromatischer Aminos?uren",
"parentId": 1,
"children": []
}, {
"id": 3,
"name": "E71.- St?rungen des Stoffwechsels verzweigter Aminos?uren und des Fetts?urestoffwechsels",
"parentId": 1,
"children": []
}, {
"id": 4,
"name": "E72.- Sonstige St?rungen des Aminos?urestoffwechsels",
"parentId": 1,
"children": []
}]
}]
}];
(function removeEmptyChildrenProperties(input){
console.log('Checking id:', input.id);
if(input.hasOwnProperty('children')){
if(input.children && input.children.length){
input.children.forEach(removeEmptyChildrenProperties)
}
else{
console.log('Remove children on id:', input.id);
delete input.children
}
};
return input
}).apply(null, tInput);
console.log(JSON.stringify(tInput));
添加回答
舉報