列表型数据转换为树行结构

131 阅读1分钟

封装函数将列表型数据转换为树行结构
list:就是要转换的数据

export function transListToTree(list, value) {
  const arr = []
  list.forEach(item => {
    if (item.pid === value) {
      // 再根据id去判断
      const children = transListToTree(list, item.id)
      if (children.length > 0) {
        item.children = children
      }
      arr.push(item)
    }
  })
  return arr
}

第二种可以下个包 Array to tree