递归

2019-12-26  本文已影响0人  sweetBoy_9126
  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);
上一篇下一篇

猜你喜欢

热点阅读