需要在javascript / jquery中为json对象提供递归函数。
“受影响”键是每个级别的叶节点。
密钥“子”可能包含任意数量的级别,其中可能包含“受影响”的节点。
我想将每个级别的叶节点(“受影响”)合并到同一级别的子(“子”)节点。
这意味着任何级别的子节点都应该在同一级别上具有受影响的数据。
输入
{
"Data": [{
"id": 1745,
"name": "Top Parent",
"parentId": null,
"child": [{
"id": 1746,
"name": "Sub Parent- child to Top Parent",
"parentId": 1745,
"child": [{
"id": 1747,
"name": "child to Sub Parent--One ",
"parentId": 1746
},
{
"id": 1748,
"name": "child to Sub Parent--two",
"parentId": 1746,
"child": [{
"id": 1749,
"name": "Child to two",
"parentId": 1748
}],
"impacted": [{
"id": 2471,
"name": "Leaf of two ",
"network": true
}]
}
],
"impacted": [{
"id": 2470,
"name": "Leaf of Sub Parent-1 ",
"network": true
},
{
"id": 2469,
"name": "Leaf of Sub Parent-1",
"network": true
}
]
}],
"impacted": [{
"id": 2468,
"name": "Leaf Of Top Parent",
"network": true
}]
}]
}
预期产出
{
"Data": [{
"id": 1745,
"name": "Top Parent",
"parentId": null,
"child": [{
"id": 1746,
"name": "Sub Parent- child to Top Parent",
"parentId": 1745,
"child": [{
"id": 1747,
"name": "child to Sub Parent--One ",
"parentId": 1746
},
{
"id": 1748,
"name": "child to Sub Parent--two",
"parentId": 1746,
"child": [{
"id": 1749,
"name": "Child to two",
"parentId": 1748
},
{
"id": 2471,
"name": "Leaf of two ",
"network": true
}
]
},
{
"id": 2470,
"name": "Leaf of Sub Parent-1 ",
"network": true
},
{
"id": 2469,
"name": "Leaf of Sub Parent-1",
"network": true
}
]
},
{
"id": 2468,
"name": "Leaf Of Top Parent",
"network": true
}
]
}]
}
即使我们不从对象中删除“受影响”的密钥也可以。但是,“受影响”的数据应该合并到同一级别的“孩子”。
我尝试了以下功能..但在每个级别获得重复
repeatLoop(data){
if(data && data.length){
for(var i=0;i<data.length;i++){
if(data[i].child && data[i].child.length && data[i].impacted && data[i].impacted.length){
for(var k=0; k<data[i].impacted.length;k++){
data[i].child.push(data[i].impacted[k])
this.repeatLoop(data[i].child)
}
}
}
}
return data
}
提前致谢
答案 0 :(得分:0)
您的代码的主要问题是您在k
的循环内递归调用该函数,而递归调用根本不依赖于k
,因此不应该调用它内循环,但在之后。
您可以使用此功能:
class X {
repeatLoop(data) {
if (!Array.isArray(data)) return;
for (var item of data) {
this.repeatLoop(item.child);
if (item.impacted) {
item.child = (item.child || []).concat(item.impacted);
delete item.impacted;
}
}
}
}
// Sample input
var data = {
"Data": [{
"id": 1745,
"name": "Top Parent",
"parentId": null,
"child": [{
"id": 1746,
"name": "Sub Parent- child to Top Parent",
"parentId": 1745,
"child": [{
"id": 1747,
"name": "child to Sub Parent--One ",
"parentId": 1746
},
{
"id": 1748,
"name": "child to Sub Parent--two",
"parentId": 1746,
"child": [{
"id": 1749,
"name": "Child to two",
"parentId": 1748
}],
"impacted": [{
"id": 2471,
"name": "Leaf of two ",
"network": true
}]
}
],
"impacted": [{
"id": 2470,
"name": "Leaf of Sub Parent-1 ",
"network": true
},
{
"id": 2469,
"name": "Leaf of Sub Parent-1",
"network": true
}
]
}],
"impacted": [{
"id": 2468,
"name": "Leaf Of Top Parent",
"network": true
}]
}]
};
// Call function
new X().repeatLoop(data.Data);
// Display mutated data:
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }