我有点想递归映射对象,这是源对象:
src =
{ “ id”:“ 41055788-0c66-4401-a391-aafa58f289aa”, “ parent_id”:“ 00000000-0000-0000-0000-000000000000”, “ organization”:“ Consejo Directivo”, “1级, “儿童”:[ { “ id”:“ 51cd732c-4a26-44e5-956c-601be17316e3”, “ parent_id”:“ 41055788-0c66-4401-a391-aafa58f289aa”, “组织”:“DirecciónGeneral”, “儿童”:[ { “ id”:“ 28cd78ff-e025-4842-a991-edde759a5a3f”, “ parent_id”:“ 51cd732c-4a26-44e5-956c-601be17316e3”, “组织”:“ Direcciones Regionales”, “儿童”:[ { “ id”:“ fbfb42f6-82c6-4887-b746-9a6dea154ab1”, “ parent_id”:“ 28cd78ff-e025-4842-a991-edde759a5a3f”, “组织”:“DireccióndeNiñez”, “儿童”:[ { “ id”:“ 32feb9d8-27c7-480a-bb64-b3430af72a3e”, “ parent_id”:“ 28cd78ff-e025-4842-a991-edde759a5a3f”, “组织”:“Direcciónde Primera Infancia”, “儿童”:[ { “ id”:“ 625aff60-af32-42e2-b0f8-581696e0f1e5”, “ parent_id”:“ 51cd732c-4a26-44e5-956c-601be17316e3”, “ organization”:“SubdirecciónGeneral”, “儿童”:[] } ] } ] } ] } ] } ] }
预期结果:
{ “数据”:{ “ id”:“ 41055788-0c66-4401-a391-aafa58f289aa”, “ parent_id”:“ 00000000-0000-0000-0000-000000000000”, “ organization”:“ Consejo Directivo”, “1级 }, “儿童”:[ { “数据”:{ “ id”:“ 51cd732c-4a26-44e5-956c-601be17316e3”, “ parent_id”:“ 41055788-0c66-4401-a391-aafa58f289aa”, “组织”:“DirecciónGeneral” }, “儿童”:[ { “数据”:{ “ id”:“ 28cd78ff-e025-4842-a991-edde759a5a3f”, “ parent_id”:“ 51cd732c-4a26-44e5-956c-601be17316e3”, “组织”:“ Direcciones Regionales” }, “儿童”:[ { “数据”:{ “ id”:“ fbfb42f6-82c6-4887-b746-9a6dea154ab1”, “ parent_id”:“ 28cd78ff-e025-4842-a991-edde759a5a3f”, “组织”:“DireccióndeNiñez” }, “儿童”:[ { “数据”:{ “ id”:“ 32feb9d8-27c7-480a-bb64-b3430af72a3e”, “ parent_id”:“ 28cd78ff-e025-4842-a991-edde759a5a3f”, “组织”:“Direcciónde Primera Infancia” }, “儿童”:[ { “数据”:{ “ id”:“ 625aff60-af32-42e2-b0f8-581696e0f1e5”, “ parent_id”:“ 51cd732c-4a26-44e5-956c-601be17316e3”, “ organization”:“SubdirecciónGeneral” }, “儿童”:[] } ] } ] } ] } ] } ] }
这是即时通讯至今没有成功的事情:
**function tranformTransverse(root, tree) {
let rmChd = Object.assign({}, root);
delete rmChd['children'];
this.dataTree.push({
data : rmChd,
children : (!!root && root.hasOwnProperty("children")) && root['children']
});
if ((!!root && root.hasOwnProperty("children")) && root['children'] instanceof Array)
root['children'].forEach(child => {
this.tranformTransverse(child, tree);
});
}**
任何想法如何达到这个结果?