gpt4 book ai didi

javascript - 创建嵌套数据(JSON 或数组)

转载 作者:行者123 更新时间:2023-12-04 02:15:38 24 4
gpt4 key购买 nike

我有以下结构的零件 list :

level | description
0 | sfg
1 | sdf
2 | asdisg
3 | sidjhf
4 | wefdsj
4 | asdfg
4 | sgfd
4 | sdg
3 | sfg
3 | fdg
4 | sdfg
4 | sdsfd
4 | sdf
4 | sdf
2 | adf
3 | sdfg
4 | sdg
4 | sdf
4 | sdf
4 | sdf
1 | shfg
2 | ijsd
3 | ijsd
4 | sdf
4 | sdf

我需要的是将此零件列表转换为这样的层次结构文件夹结构(在嵌套 JSON 中首选):
enter image description here

我想在 TypeScript 中使用以下结构:
export interface Foo {
level: number;
description: string;
children?: Foo[];
}

我需要的是一个嵌套的 JSON 对象或类似嵌套数组的东西,其中数组包含另一个数组的子元素。

不幸的是,我只有 伪代码 展示(我知道,这永远不会奏效):
  recursiveWalker(List, lastLevel, parent) {
for (let i = 0; i < List.length; ++i) {
const node = {
description: List.description,
level: List.level
};
if (List.level === lastLevel + 1) {
node.children = this.recursiveWalker(List, lastLevel + 2, node);
}
}
return node;
}

我现在的问题是:是否有创建此类嵌套数据的简单解决方案?我必须使用递归函数,还是有什么算法可以实现?

目标是使用这些数据在 Angular ( https://material.angular.io/components/tree/overview) 中创建一个 Material 树。

我真的很感激任何帮助!

更新:
将需要以下结构:
  const bar: Foo = {
description: 'this is level 0 (root)',
level: 0,
children: [{
description: 'this is level 1',
level: 1,
children: [
{
description: 'this is level 2',
level: 2
},
{
description: 'this is level 2',
level: 2
},
{
description: 'this is level 2',
level: 2,
children: [
{
description: 'this is level 3',
level: 3
},
{
description: 'this is level 3',
level: 3
},
{
description: 'this is level 3',
level: 3,
children: [] /* AND SO ON .... */
}
]
}
]
}]
};
});

最佳答案

您可以通过使用数组 levels 来采取惰性方法。作为跟踪实际关卡最新节点的助手。

var data = [{ level: 0, description: '0' }, { level: 1, description: '0-0' }, { level: 2, description: '0-0-0' }, { level: 3, description: '0-0-0-0' }, { level: 4, description: '0-0-0-0-0' }, { level: 4, description: '0-0-0-0-1' }, { level: 4, description: '0-0-0-0-2' }, { level: 4, description: '0-0-0-0-3' }, { level: 3, description: '0-0-0-1' }, { level: 3, description: '0-0-0-2' }, { level: 4, description: '0-0-0-2-0' }, { level: 4, description: '0-0-0-2-1' }, { level: 4, description: '0-0-0-2-2' }, { level: 4, description: '0-0-0-2-3' }, { level: 2, description: '0-0-1' }, { level: 3, description: '0-0-1-0' }, { level: 4, description: '0-0-1-0-0' }, { level: 4, description: '0-0-1-0-1' }, { level: 4, description: '0-0-1-0-2' }, { level: 4, description: '0-0-1-0-3' }, { level: 1, description: '0-1' }, { level: 2, description: '0-1-0' }, { level: 3, description: '0-1-0-0' }, { level: 4, description: '0-1-0-0-0' }, { level: 4, description: '0-1-0-0-1' }],
tree = [],
levels = [tree];

data.forEach(({ level, description }) =>
levels[level].push({ level, description, children: levels[level + 1] = [] })
);

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

关于javascript - 创建嵌套数据(JSON 或数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58970614/

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