递归

作者: sweetBoy_9126 | 来源:发表于2019-12-26 15:33 被阅读0次
    1. 对每一个children添加一个level,来表示它的层级
    function deepGetValueItem(item, index) {
          let deepIndex = index || 0
          deepIndex += 1
          for (let i = 0; i < item.length; i++) {
            item[i]['level'] = deepIndex
            if (item[i].children) deepGetValueItem(item[i].children, deepIndex)
          }
        }
    
    1. 每一层级下都与最外层的索引一致
    function treeData(arr, sIndex = undefined) {
      for (let i = 0; i < arr.length; i++) {
        arr[i]._index = sIndex || String(i);
        if (arr[i].children && arr[i].children.length > 0) {
          treeData(arr[i].children, arr[i]._index);
        }
      }
    }
    treeData(data);
    

    相关文章

      网友评论

          本文标题:递归

          本文链接:https://www.haomeiwen.com/subject/bwxloctx.html