gpt4 book ai didi

Javascript父子树合并

转载 作者:行者123 更新时间:2023-11-28 06:16:58 26 4
gpt4 key购买 nike

下面是三个数组,我想使用 mergelist 函数合并它们。我是 javascript 新手,请帮助我。

var list1 = [
{name: 'Parent'},
{name: 'child1', parent: ‘parent’},
{name: 'child2', parent: ‘parent’},
{name: 'child21', parent: 'child2'}
];

var list2 = [
{name: 'child1'},
{name: 'child11', parent: 'child1'},
{name: 'child12', parent: 'child1'}
];

var list3 = [
{name: 'child2'},
{name: 'child22', parent: 'child2'},
{name: 'child23', parent: 'child2'}
];

实现一个函数,将数组中的所有树合并为一棵组合树。

请帮我编写代码。谢谢我已经尝试过使用这段代码,但我只能对 2 棵树执行此操作,而不是我想要的。

var list1 = [
{
Parent: 'parent',
children: [
{
parent: 'child1',
children: []
},
{
parent: 'child2',
children: [
{
parent:'child21',
children :[]
}]
}]
}
];

var list2 = [
{
parent: 'child1',
children: [
{
parent: 'child11',
children: []
},
{
parent: 'child12',
children: []
}]
}
];

var list3 = [
{
parent: 'child2',
children: [
{
parent: 'child22',
children: []
},
{
parent: 'child23',
children: []
}]
}
]
var addNode = function(nodeId, array) {
array.push({parent: nodeId, children: []});
};

var placeNodeInTree = function(nodeId, parent, treeList) {
return treeList.some(function(currentNode){

// If currentNode has the same id as the node we want to insert, good! Required for root nodes.
if(currentNode.parent === nodeId) {
return true;
}

// Is currentNode the parent of the node we want to insert?
if(currentNode.parent === parent) {

// If the element does not exist as child of currentNode, create it
if(!currentNode.children.some(function(currentChild) {
return currentChild.parent === nodeId;
})) addNode(nodeId, currentNode.children);

return true;
} else {

// Continue looking further down the tree
return placeNodeInTree(nodeId, parent, currentNode.children);
}
});
};

var mergeInto = function(tree, mergeTarget, parentId) {
parentId = parentId || undefined;
tree.forEach(function(node) {

// If parent has not been found, placeNodeInTree() returns false --> insert as root element
if(!placeNodeInTree(node.parent, parentId, mergeTarget)){
list1.push({parent: node.parent, children:[]});
}

mergeInto(node.children, mergeTarget, node.parent);

});
};

mergeInto(list2, list1);

document.write('<pre>');
document.write(JSON.stringify(list1, null, 4));
console.log(list1);
document.write('</pre>');

最佳答案

这看起来是一个简单的问题,但数据源具有误导性,重复项有时具有父属性,有时不具有。

第一部分是清理数据并构建一个具有单一名称的平面数组,如果可能的话还可以使用父数组。数据存储在data中。第一个输出就是这个结果。

第二部分是用数据构建一棵树。对于每个对象,都会构建一个 name 节点,并为每个给定的 parent 创建一个节点(如果该节点尚不存在)。

var list1 = [{ name: 'parent' }, { name: 'child1', parent: 'parent' }, { name: 'child2', parent: 'parent' }, { name: 'child21', parent: 'child2' }],
list2 = [{ name: 'child1' }, { name: 'child11', parent: 'child1' }, { name: 'child12', parent: 'child1' }],
list3 = [{ name: 'child2' }, { name: 'child22', parent: 'child2' }, { name: 'child23', parent: 'child2' }],
data = function (data) {
var o = {}, r = [];
data.forEach(function (a) {
if (!o[a.name]) {
r.push(a);
o[a.name] = a;
return;
}
if (!(parent in o[a.name])) {
o[a.name] = a;
}
});
return r;
}([].concat(list1, list2, list3)),
tree = function (data) {
var r, o = {};
document.write('<pre>data: ' + JSON.stringify(data, 0, 4) + '</pre>');
data.forEach(function (a, i) {
a.children = o[a.name] && o[a.name].children;
o[a.name] = a;
if (a.parent === undefined) {
r = a;
return;
}
o[a.parent] = o[a.parent] || {};
o[a.parent].children = o[a.parent].children || [];
o[a.parent].children.push(a);
});
return r;
}(data);

document.write('<pre>tree: ' + JSON.stringify(tree, 0, 4) + '</pre>');

关于Javascript父子树合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35882884/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com