文章结构

遍历一个树形结构,并为每一个子树添加其所在树节点相应的叶节点下标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);
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐