[英]Can I remove a redundant data structure wrapper from my nested array to nested json script?
I wrote this script to convert a nested array with the structure below to a nested object with parent child relationships. 我编写了这个脚本,将具有下面结构的嵌套数组转换为具有父子关系的嵌套对象。
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'],
];
It seems to do the trick, but in order to prime the script I've had to add data.children
wrapper around the initial data structure. 它似乎可以解决问题,但是为了
data.children
脚本,我必须在初始数据结构周围添加data.children
包装器。 I'm not convinced it is needed, though I haven't been able to workout how to get rid of it. 我不相信这是必要的,尽管我还没有能够锻炼如何摆脱它。
Can anyone see anything I'm missing? 任何人都能看到我失踪的东西吗?
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];
}
Sample Output 样本输出
{
"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": []
}
]
}
]
}
]
}
]
}
The for
loops can be cleaned up by extracting some functionality to named functions. 可以通过向命名函数提取一些功能来清除
for
循环。
const node = (name, parent = null) => ({name, parent, children: []})
handles creating a node. const node = (name, parent = null) => ({name, parent, children: []})
处理创建节点。
Nodes can then be added with addNode()
然后可以使用
addNode()
添加节点
To search for the current next parent node findNamedNode()
搜索当前下一个父节点
findNamedNode()
If a node
with the current
name is found it moves down to the next node
. 如果找到具有
current
名称的node
,则它将向下移动到下一个node
。 If no node
exists with the current
name it is created. 如果不存在具有
current
名称的node
,则创建该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;
}
Thanks to the help from @Blindman67 on Code Review. 感谢@ Blindman67对Code Review的帮助。
https://codereview.stackexchange.com/questions/219418/convert-nested-array-of-values-to-a-tree-structure/ https://codereview.stackexchange.com/questions/219418/convert-nested-array-of-values-to-a-tree-structure/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.