使用js递归来遍历树形结构的数据
遍历一个树形结构,并为每一个子树添加其所在树节点相应的叶节点下标indexconst data = [{name: 'a',age: 1,children: [{name: 'a1',age: 1,},{name: 'a2',age: 1,
·
文章结构
遍历一个树形结构,并为每一个子树添加其所在树节点相应的叶节点下标index
const data = [{
name: 'a',
age: 1,
children: [{
name: 'a1',
age: 1,
},
{
name: 'a2',
age: 1,
children: [{
name: 'a21',
age: 1,
},
{
name: 'a22',
age: 1,
},
]
},
]
},
{
name: 'b',
age: 1,
children: [{
name: 'b1',
age: 1,
children: [{
name: 'b11',
age: 1,
},
{
name: 'b12',
age: 1,
},
]
},
{
name: 'b2',
age: 1,
children: [{
name: 'b22',
age: 1,
},
{
name: 'b22',
age: 1,
children: [{
name: 'b21',
age: 1,
},
{
name: 'b22',
age: 1,
},
{
name: 'b23',
age: 1,
},
]
},
]
},
]
},
]
const resultData = [{
name: 'a',
age: 1,
index: 0,
children: [{
name: 'a1',
age: 1,
index: 0,
},
{
name: 'a2',
age: 1,
index: 1,
children: [{
name: 'a21',
age: 1,
index: 0,
},
{
name: 'a22',
age: 1,
index: 1,
},
]
},
]
},
{
name: 'b',
age: 1,
index: 1,
children: [{
name: 'b1',
age: 1,
index: 0,
children: [{
name: 'b11',
age: 1,
index: 0,
},
{
name: 'b12',
age: 1,
index: 1,
},
]
},
{
name: 'b2',
age: 1,
index: 1,
children: [{
name: 'b22',
age: 1,
index: 0,
},
{
name: 'b22',
age: 1,
index: 1,
children: [{
name: 'b21',
age: 1,
index: 0,
},
{
name: 'b22',
age: 1,
index: 1,
},
{
name: 'b23',
age: 1,
index: 2,
},
]
},
]
},
]
}
]
let count = 0;
const main = function(data) {
for (let i in data) {
data[i].index = i;
if (data[i].children) {
count++;
main(data[i].children);
}
}
}
main(data);
console.log(data);
console.log(count);
更多推荐
已为社区贡献4条内容
所有评论(0)