如何在Javascript中从数组创建树(父子对象) [英] How to create a tree (parent-child) object from Array in Javascript
问题描述
我有一个像这样的数组
[
"parent1|child1|subChild1",
"parent1|child1|subChild2",
"parent|child2|subChild1",
"parent1|child2|subChild2",
"parent2|child1|subChild1",
"parent2|child1|subChild2",
"parent2|child2|subChild1",
.
.
.
]
其中,我在|
之前的第一个字符串是父级,而在| c之前的第二个字符串是是子项,第二个|
之后的第三个字符串是子子项
Wherein my first string before |
is the parent and the second string before | is the child and the third string after the second |
is the subchild
如何将该数组转换为类似对象
How can I convert this array into an object like
[
{
"id": "parent1",
"children":[
{
"id": "child1",
"children":[
{
"id": "subChild1"
}
]
}
]
}
]
父->子->子子对象
基于塞巴斯蒂安的回答,我在下面使用打字稿尝试了
Based on Sebastian's answer I tried below using typescript
private genTree(row) {
let self = this;
if (!row) {
return;
}
const [parent, ...children] = row.split('|');
if (!children || children.length === 0) {
return [{
id: parent,
children: []
}];
}
return [{
id: parent,
children: self.genTree(children.join('|'))
}];
}
private mergeDeep(children) {
let self = this;
const res = children.reduce((result, curr) => {
const entry = curr;
const existing = result.find((e) => e.id === entry.id);
if (existing) {
existing.children = [].concat(existing.children, entry.children);
} else {
result.push(entry);
}
return result;
}, []);
for (let i = 0; i < res.length; i++) {
const entry = res[i];
if (entry.children && entry.children.length > 0) {
entry.children = self.mergeDeep(entry.children);
}
};
return res;
}
private constructTree(statKeyNames){
let self = this;
const res = this.mergeDeep(statKeyNames.map(self.genTree).map(([e]) => e));
console.log(res);
}
但这给了我
无法读取未定义的属性'genTree'"
Cannot read property 'genTree' of undefined" error
更新:
根据塞巴斯蒂安的评论,self.genTree
更改为this.genTree.bind(this)
,并且没有任何问题
As per Sebastian's comment changed self.genTree
to this.genTree.bind(this)
and it worked without any issues
推荐答案
您必须为此使用递归.在这里看看:
You have to use recursion for that. Take a look here:
const arr = [
"parent1|child1|subChild1",
"parent1|child1|subChild2",
"parent|child2|subChild1",
"parent1|child2|subChild2",
"parent2|child1|subChild1",
"parent2|child1|subChild2",
"parent2|child2|subChild1"
];
function genTree(row) {
const [parent, ...children] = row.split('|');
if (!children || children.length === 0) {
return [{
id: parent,
children: []
}];
}
return [{
id: parent,
children: genTree(children.join('|'))
}];
};
function mergeDeep(children) {
const res = children.reduce((result, curr) => {
const entry = curr;
const existing = result.find((e) => e.id === entry.id);
if (existing) {
existing.children = [].concat(existing.children, entry.children);
} else {
result.push(entry);
}
return result;
}, []);
for (let i = 0; i < res.length; i++) {
const entry = res[i];
if (entry.children && entry.children.length > 0) {
entry.children = mergeDeep(entry.children);
}
};
return res;
}
const res = mergeDeep(arr.map(genTree).map(([e]) => e));
console.log(JSON.stringify(res, false, 2));
我在这里使用了两个帮助器:genTree(row)
递归地从每一行生成一个简单的树,以及mergeDeep(children)
减少arr.map(genTree).map(([e]) => e)
结果中的第一级树,然后遍历数组并递归地执行每个条目的所有children
都具有相同的含义.
I used two helpers here: genTree(row)
which recursively generates a simple tree from each row, and mergeDeep(children)
which reduces the first-level trees in the result of arr.map(genTree).map(([e]) => e)
, and then iterates over the array and recursively does the same thing to all children
of each entry.
这篇关于如何在Javascript中从数组创建树(父子对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!