3 回答
TA贡献1843条经验 获得超7个赞
function transilate(data) {
return data.map((item) => {
if (item.children2) {
if(item.children2.length>0){
let concatR=item.children1?item.children1.concat(item.children2):item.children2
item.children1=concatR
}
delete item.children2
item.children1 = transilate(item.children1)
}
return item
});
}
TA贡献1946条经验 获得超3个赞
题主的 result 给的有错啊,,,
let test = JSON.parse(JSON.stringify(data));
function childrenIterator(arr) {
arr.map(function(obj) {
if (obj.children2 && obj.children2.length !== 0) {
obj.children2.forEach(function(o) {
obj.children1 ? obj.children1.push(o) : obj.children1 = [o];
});
}
delete obj.children2;
if (obj.children1) {
childrenIterator(obj.children1);
}
return obj;
});
return arr;
}
console.log(JSON.stringify(childrenIterator(test)));
添加回答
举报