js、vue 树状图 添加 索引值

https://img3.mukewang.com/5b684f580001298f07520477.jpg

{

        "name": "第一层",

        index:1,

        "children":[

            {

                "name":"第二层",

                index:2,

            },{

                "name":"第二层",

                index:2,

                "children":[

                    {

                        "name":"第三层",

                        index:3,

                    },{

                        "name":"第三层",

                        index:3,

                    },

                ]

            }

        ]

    }

    

想在这个树状图中添加他们每一层的index,

万千封印
浏览 846回答 1
1回答

冉冉说

最终写法。    function fun(arry){        for(let i in arry){            let menu = arry[i];            if(!menu['num']){                menu['num'] = 1;            }            if(menu['children'].length && menu['children']){                for(let j in menu['children']){                    menu['children'][j]['num'] = arry[i]['num'] + 1;                    fun(menu['children'])                }            }        }    }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Vue.js