let arr = [{
      name: "1",
      children: [{
          name: " 1 - 1",
        },
        {
          name: " 1 - 2",
          children: [{
            name: "1 - 2 - 1",
          }, ]
        }
      ]
    }, {
      name: "2",
      children: [{
          name: "2 - 1",
        },
        {
          name: "2 - 2",
          children: [{
            name: "2 - 2 - 1",
          }, ]
        }
      ]
    }]

    function test(arr, list = []) {
      let children = [];
      arr.forEach(a => {
        list.push(a.name);
        a.children && children.push(...a.children);
      });
      return children.length ? test(children, list) : list
    }
    console.log(test(arr))

  

 posted on 2019-03-28 09:54  落逸  阅读(105)  评论(0编辑  收藏  举报