gpt4 book ai didi

javascript - 我可以从嵌套数组中删除冗余数据结构包装器到嵌套 json 脚本吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:37:10 25 4
gpt4 key购买 nike

我编写此脚本是为了将具有以下结构的嵌套数组转换为具有父子关系的嵌套对象。

list = [
['lvl-1 item-1', 'lvl-2 item-1'],
['lvl-1 item-1', 'lvl-2 item-1', 'lvl-3 item-1'],
['lvl-1 item-1', 'lvl-2 item-1', 'lvl-3 item-2'],
['lvl-1 item-2', 'lvl-2 item-1', 'lvl-3 item-1'],
['lvl-1 item-2', 'lvl-2 item-2', 'lvl-3 item-2', 'lvl-4 item-1'],
];

它似乎可以解决问题,但为了启动脚本,我不得不在初始数据结构周围添加 data.children 包装器。我不相信它是必要的,尽管我一直无法锻炼如何摆脱它。

任何人都可以看到我遗漏的任何东西吗?

console.log(nestedArrayToJson(list));

function nestedArrayToJson(structure) {
const top_item = '0';

// This was added to behave like the child data structure.
let data = {
children: [
{
name: top_item,
parent: null,
children: [],
}],
};

for(let i = 0; i < structure.length; i++) {
let parents = [top_item];
for(let j = 0; j < structure[i].length; j++) {
let obj = data;
for(parent of parents) {
obj = obj.children.find(o => o.name === parent);
}
const name = structure[i][j];
if(!obj.children.find(o => o.name === name)) {
obj.children.push({
name,
parent,
children: [],
});
}
parents.push(structure[i][j]);
}
}

return data.children[0];
}

示例输出

{
"name": "0",
"parent": null,
"children": [
{
"name": "lvl-1 item-1",
"parent": "0",
"children": [
{
"name": "lvl-2 item-1",
"parent": "lvl-1 item-1",
"children": [
{
"name": "lvl-3 item-1",
"parent": "lvl-2 item-1",
"children": []
},
{
"name": "lvl-3 item-2",
"parent": "lvl-2 item-1",
"children": []
}
]
}
]
},
{
"name": "lvl-1 item-2",
"parent": "0",
"children": [
{
"name": "lvl-2 item-1",
"parent": "lvl-1 item-2",
"children": [
{
"name": "lvl-3 item-1",
"parent": "lvl-2 item-1",
"children": []
}
]
},
{
"name": "lvl-2 item-2",
"parent": "lvl-1 item-2",
"children": [
{
"name": "lvl-3 item-2",
"parent": "lvl-2 item-2",
"children": [
{
"name": "lvl-4 item-1",
"parent": "lvl-3 item-2",
"children": []
}
]
}
]
}
]
}
]
}

最佳答案

可以通过将一些功能提取到命名函数来清理 for 循环。

const node = (name, parent = null) => ({name, parent, children: []}) 处理节点的创建。

然后可以使用 addNode() 添加节点

寻找当前的下一个父节点findNamedNode()

如果找到具有当前 名称的节点,它会向下移动到下一个节点。如果不存在具有 current 名称的 node,则创建它。

function createTree(arr, topItem = 'Top') {
const node = (name, parent = null) => ({name, parent, children: []});
const addNode = (parent, child) => {
parent.children.push(child);

return child;
};
const findNamedNode = (name, parent) => {
for(const child of parent.children) {
if(child.name === name) { return child; }
const found = findNamedNode(name, child);
if(found) { return found; }
}
};

const top = node(topItem);
let current;

for(const children of arr) {
current = top;
for(const name of children) {
const found = findNamedNode(name, current);
current = found ? found : addNode(current,
node(name, current.name));
}
}

return top;
}

感谢 @Blindman67 在 Code Review 上的帮助。

https://codereview.stackexchange.com/questions/219418/convert-nested-array-of-values-to-a-tree-structure/

关于javascript - 我可以从嵌套数组中删除冗余数据结构包装器到嵌套 json 脚本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55922738/

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