我有 3 个不同的对象(obj1,2,3),如下所示,我正在尝试形成 finalObj,它是 3 个对象的并集(唯一树列表)..
我不确定实现这一目标的最佳方法是什么。
编辑:文本是这里的关键。如果已经存在具有相同文本的父节点,我们只需忽略并移动到其子节点并检查该子节点是否存在,否则将该子节点附加到现有父节点等等......
var obj1 = [{
text: "TreeRoot",
items: [
{ text: "Subgroup1"},
{ text: "Subgroup2"}
]
}]
var obj2 = [{
text: "TreeRoot",
items: [
{ text: "Subgroup3"}
]
}]
var obj3 = [{
text: "Subgroup3",
items: [{
text: "subgroup5",
items: [{
text: "subgroup6",
items: [{
text: "subgroup7",
items: [{
text: "subgroup8"
}]
}]
}]
}]
}]
var finalObj = [{
text: "TreeRoot",
items: [
{ text: "Subgroup1"},
{ text: "Subgroup2"},
{
text: "Subgroup3",
items: [{
text: "subgroup5",
items: [{
text: "subgroup6",
items: [{
text: "subgroup7",
items: [{
text: "subgroup8"
}]
}]
}]
}]
}
]
}]