gpt4 book ai didi

javascript - 在javascript中创建树的 trim 副本

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:12:57 24 4
gpt4 key购买 nike

我正在尝试在我拥有源数据/树的下方创建树的 trim 版本:

const treeData = [{
title: '0-0',
key: '0-0',
children: [{
title: '0-0-0',
key: '0-0-0',
children: [
{ title: '0-0-0-0', key: '0-0-0-0', children: [] },
{ title: '0-0-0-1', key: '0-0-0-1', children: [] },
{ title: '0-0-0-2', key: '0-0-0-2', children: [] },
],
}, {
title: '0-0-1',
key: '0-0-1',
children: [
{ title: '0-0-1-0', key: '0-0-1-0', children: [] },
{ title: '0-0-1-1', key: '0-0-1-1', children: [] },
{ title: '0-0-1-2', key: '0-0-1-2', children: [] },
],
}, {
title: '0-0-2',
key: '0-0-2',
children: []
}],
}, {
title: '0-1',
key: '0-1',
children: [
{ title: '0-1-0-0', key: '0-1-0-0', children: [] },
{ title: '0-1-0-1', key: '0-1-0-1', children: [] },
{ title: '0-1-0-2', key: '0-1-0-2', children: [] },
],
}, {
title: '0-2',
key: '0-2',
children: []
}];

和叶节点数组作为输入。

const leafNodes = ['0-0-1-2', '0-1-0-1', '0-1-0-2']

鉴于此输入,我希望这棵 trim 后的树使用叶节点构建从根到每个叶的所有路径:

const pruned [{
title: '0-0',
key: '0-0',
children: [{
title: '0-0-1',
key: '0-0-1',
children: [
{ title: '0-0-1-2',
key: '0-0-1-2',
children: []
}
]
}]
}, {
title: '0-1',
key: '0-1',
children: [{
title: '0-1-0-1',
key: '0-1-0-1',
children: []
}, {
title: '0-1-0-2',
key: '0-1-0-2',
children: []
}]
}]

我正在考虑逐个节点构建复制节点,而不是复制数据源,然后根据叶节点的数组/列表移除不可构建的路径,因为我认为出于可维护性目的,这将是最容易理解的,但是即便如此,我还是对如何协调这个过程感到困惑,尤其是在考虑已经添加到我的复制树中的中间节点时,就像“0-1-0-1”和“0-1-0-2'。无论如何,我被难住了一段时间并举起手来。引用的代码是 javascript,但我愿意接受与 javascript 足够相似的其他语言的答案。

最佳答案

您可以通过找到目标键来构建新的数组/对象,并通过返回具有必要节点的数组来收集所有对象。

function getParts(array, leafes) {
var result = [];
array.forEach(o => {
var children;
if (leafes.includes(o.key)) {
result.push(o);
return;
}
children = getParts(o.children, leafes);
if (children.length) {
result.push(Object.assign({}, o, { children }));
}
});
return result;
}

const
treeData = [{ title: '0-0', key: '0-0', children: [{ title: '0-0-0', key: '0-0-0', children: [{ title: '0-0-0-0', key: '0-0-0-0', children: [] }, { title: '0-0-0-1', key: '0-0-0-1', children: [] }, { title: '0-0-0-2', key: '0-0-0-2', children: [] }] }, { title: '0-0-1', key: '0-0-1', children: [{ title: '0-0-1-0', key: '0-0-1-0', children: [] }, { title: '0-0-1-1', key: '0-0-1-1', children: [] }, { title: '0-0-1-2', key: '0-0-1-2', children: [] }] }, { title: '0-0-2', key: '0-0-2', children: [] }] }, { title: '0-1', key: '0-1', children: [{ title: '0-1-0-0', key: '0-1-0-0', children: [] }, { title: '0-1-0-1', key: '0-1-0-1', children: [] }, { title: '0-1-0-2', key: '0-1-0-2', children: [] }] }, { title: '0-2', key: '0-2', children: [] }],
leafNodes = ['0-0-1-2', '0-1-0-1', '0-1-0-2'],
pruned = getParts(treeData, leafNodes);

console.log(pruned);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 在javascript中创建树的 trim 副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55090371/

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