【发布时间】:2020-05-24 02:00:24
【问题描述】:
我有点卡在递归映射对象上,这是源对象:
源数据:
{
"id": "41055788",
"parent_id": "00000000",
"organization": "Consejo Directivo",
"level": 1,
"children": [
{
"id": "51cd732c",
"parent_id": "41055788",
"organization": "Dirección General",
"children": [
{
"id": "28cd78ff",
"parent_id": "51cd732c",
"organization": "Direcciones Regionales",
"children": []
....
** 我一直在试图弄清楚如何遍历树和预期的结果:**
{
"data":{
"id": "41055788",
"parent_id": "00000000",
"organization": "Consejo Directivo",
"level": 1
},
"children": [
{
"data": {
"id": "51cd732c",
"parent_id": "41055788",
"organization": "Dirección General"
},
"children": [ ] ...
这是我到现在为止没有成功的:
**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);
});
}**
任何想法如何达到这个结果?
【问题讨论】:
-
这能回答你的问题吗? looping through an object (tree) recursively
标签: javascript recursion