用js实现将一个二维数组转换成树结构。
例如:将下面的二维数组:
var arr = [
["a", "aa", "aaa", "aaaa"],
["b", "bb", "bbb"],
["a", "ab", "aba"],
["a", "aa", "aab"]
]
转成下面树结构的对象数组:
[{
"name" : "a",
"child" : [
{
"name" : "aa",
"child" : [
{
"name" : "aaa",
"child" : [
{
"name" : "aaaa",
"child" : []
}
]
},
{
"name" : "aab",
"child" : []
}
]
},
{
"name" : "ab",
"child" : [
{
"name": "aba",
"child" : []
}
]
}
]
},
{
"name": "b",
"child" : [
{
"name" : "bb",
"child" : [
{
"name" : "bbb",
"child" : []
}
]
}
]
}]
代码如下:
function toTree(arr) {
const obj = {};
const res = [];
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
const item = arr[i][j];
if (!obj[item]) {
obj[item] = {
name: item,
child: [],
};
}
if (j > 0) {
const parent = obj[arr[i][j - 1]];
if (parent) {
if (parent.child.indexOf(obj[item]) < 0) {
parent.child.push(obj[item]);
}
}
} else {
if (res.indexOf(obj[item]) < 0) {
res.push(obj[item]);
}
}
}
}
return res;
}
console.log(toTree(arr))
最终转换结果为: