Javascript 遍历Tree获取指定键值的索引

时间:2021-03-14 19:17:46

[
    {
        "id":1,
        "name":"A",
        "children":[
            {
                "id":2,
                "name":"a",
                "children":[
                    {
                        "id":3,
                        "name":"a_1",
                        "children":[
                            {
                                "id":4,
                                "name":"a_1_1",
                                "children":[

                                ]
                            }
                        ]
                    },
                    {
                        "id":5,
                        "name":"a_2",
                        "children":[

                        ]
                    }
                ]
            },
            {
                "id":7,
                "name":"b",
                "children":[

                ]
            }
        ]
    },
    {
        "id":8,
        "name":"B",
        "children":[

        ]
    }
]

上为JSON实例,输入参数 id = 5, 能够获取该ID位置的索引回路值:0-0-1,感谢

2 个解决方案

#1


我要获取id值为5的对象的索引值哇,在线求教,谢谢啊

#2


我获取这个ID但是我想动态修改这个name,所以我得获取它在父对象里的索引值哦

#1


我要获取id值为5的对象的索引值哇,在线求教,谢谢啊

#2


我获取这个ID但是我想动态修改这个name,所以我得获取它在父对象里的索引值哦