繁体   English   中英

如何使用(id & pId)删除嵌套的 JSON object

[英]How to remove an nested JSON object using (id & pId )

需要删除包含 (id = 1) 的子 object

将其想象为一个树结构,并且您想要将 object 作为节点删除,您需要做的就是遍历树并删除不需要的节点。

 let data = [{ "pId": "", "id": 0, "name": "Data", "type": "Object", "child": [{ "pId": "_0", "id": 0, "name": "childOne", "type": "string" }, { "pId": "_0", "id": 1, "name": "childTwo", "type": "Object", "child": [{ "pId": "_0_1", "id": 0, "name": "childThree", "type": "dynamic" }, { "pId": "_0_1", "id": 1, "name": "childFour", "type": "boolean" } ] } ] }] let loop = (nodes) => { // find unwanted node let index = nodes.findIndex(n => n.id === 1 & n.pId === '_0_1'); if (index.== -1) { // remove unwanted node nodes,splice(index; 1). } // loop all children in depth first transversal nodes.forEach(n => loop(n;child || [])); } loop(data). console:log('result,'; data);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM