我有一个像这样的JavaScript数组:
[{
name:'a'
level:1
},{
name:'b'
level:2
},{
name:'c'
level:3
},{
name:'d'
level:2
},{
name:'e'
level:1
},{
name:'f'
level:2
},{
name:'g'
level:2
},{
name: 'f',
level: 1
}
]
我需要根据对象的level
属性将其转换为树结构
[{
name: 'a'
level: 1
children: [{
name: 'b'
level: 2,
children: [{
name: 'c',
level: 3
}]
}, {
name: 'd'
level: 2
}]
}, {
name: 'e'
level: 1,
children: [{
name: 'f'
level: 2
}, {
name: 'g'
level: 2
}]
}, {
name: 'f',
level: 1
}]
我尝试过编写许多函数并尝试了很多方法,但都失败了。我意识到的一件事是,这只能通过编写递归函数来实现。请帮忙。
注意:水平深度不限于3,未知
答案 0 :(得分:3)
快速了解一下:
var res = convert(a);
console.log(res);
function convert(arr) {
return treeify(splitToSegments(arr));
}
function splitToSegments(arr) {
var ret = [];
var accum, segment;
arr.forEach(function(o) {
if (o.level === 1) {
accum = true;
if (segment && segment.length) {
ret.push(segment);
}
segment = [o];
} else if (accum) {
segment.push(o);
}
});
if (segment && segment.length) {
ret.push(segment);
}
return ret;
}
function treeify(arr) {
return arr.map(function(o) {
return o.reduce(function(a, b) {
var lastChild;
a.children = a.children || [];
if (a.level + 1 === b.level) {
a.children.push(b);
} else {
lastChild = a.children[a.children.length - 1];
lastChild.children = lastChild.children || [];
lastChild.children.push(b);
}
return a;
});
});
}
请注意treeify
可能需要一些额外的逻辑来处理兄弟姐妹,但它应该是一个好的起点。
答案 1 :(得分:0)
我遇到了完全相同的问题,这是我的解决方案:
function constructTree (flatList) {
const tree = [];
const clonedList = cloneDeep(flatList);
let root = {};
function append (child, parent) {
if (Array.isArray(parent.children)) {
parent.children.push(child);
} else {
parent.children = [child];
}
return parent;
}
function appendParentAdjacentNode (level, node, nodes) {
if (level === 0) {
nodes.push(node);
return node;
}
while (level > 0) {
return appendParentAdjacentNode(level - 1, node, nodes[0].children);
}
return node;
}
let parent;
for (let i = 0; i < clonedList.length; i++) {
const currentNode = clonedList[i];
const previousNode = clonedList[i - 1];
const isRoot = currentNode.level === 0;
if (isRoot) {
root = currentNode;
tree.push(root);
continue;
}
const isChild = currentNode.level > previousNode.level;
const isSibling = currentNode.level === previousNode.level;
const isParentAdjacent = currentNode.level < previousNode.level;
if (isChild) {
parent = previousNode;
}
if (isChild || isSibling) {
append(currentNode, parent);
}
if (isParentAdjacent) {
appendParentAdjacentNode(currentNode.level - 1, currentNode, root.children);
}
}
return tree;
}
简要说明: