js 数组中多层嵌套对象,怎么递归取得,某id 对象的路径

数组中多层嵌套对象,怎么递归取得,某id 对象的路径

  const arr =[
    { id:'1',
      title:'a',
      children:[
        {id:'33',
         title:'axx',
         children:[{ id:'121111',title:'hh'},] 
        },
        {id:'34',title:'xxddf',}
      ]
    },
    { id:'2',title:'bxx'}
     //  略...
  ]
 // 找出 id为 121111 对象的路径, 返回一个数组: [第一层的 title,第三层的title ,第n层的 title ,自已的 title]

下面这样写不行,只能找到自已这层,没有父级路径


  function pathCompute(arr, key, path = []){
    for( let i = 0, len = arr.length; i < len; i++){
      path.push(arr[i].title)
      if( arr[i].key === key){
         console.log('找到了', path)
         return path
      } else if(arr[i].children && arr[i].children.length >= 1){
        pathCompute(arr[i].children, key, path)
      } else {
        path = [] // 没找到就清空数组,下循环重新写路径,但这样写不行
      }
    }
  }



你题目的解答代码如下:

  const arr =[
    { id:'1',
      title:'a',
      children:[
        {id:'33',
         title:'axx',
         children:[{ id:'121111',title:'hh'},] 
        },
        {id:'34',title:'xxddf',}
      ]
    },
    { id:'2',title:'bxx'}
     //  略...
]
// 找出 id121111 对象的路径, 返回一个数组: [第一层的 title,第三层的title ,第n层的 title ,自已的 title]
 
function pathCompute(arr, key, path = []){
    for( let i = 0, len = arr.length; i < len; i++){
        path.push(arr[i].title);
        if( arr[i].id === key){
            console.log('找到了');
            return path;
        } else if(arr[i].children && arr[i].children.length >= 1){
            if(pathCompute(arr[i].children, key, path)!=false)
                return path;
          }
        path.pop();
    }
    return false;
}
console.log(pathCompute(arr, '121111')); 

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      const arr = [
        {
          id: "1",
          title: "a",
          children: [
            {
              id: "33",
              title: "axx",
              children: [
                {
                  id: "121115",
                  title: "hh",
                  children: [{ id: "121111", title: "hh1" }],
                },
              ],
            },
            { id: "34", title: "xxddf" },
          ],
        },
        { id: "2", title: "bxx" },
        //  略...
      ];

      let result = [];

      function arrType(arr, key, trage) {
        return arr.find((item) => {
          if (item.constructor === Object) {
            if (item[key] == trage) {
              return item;
            }

            if (Array.isArray(item.children)) {
              var zitem = arrType(item.children, key, trage)&&arrType(item.children, key, trage).title;
              if (zitem &&result.indexOf(zitem) == -1) {
                result.unshift(zitem);
              }
              return arguments.callee(item.children, key, trage);
            }
          }
        });
      }
      result.unshift(arrType(arr, "id", 121111).title);
      console.log(result, "====");
    </script>
  </body>
</html>