export function arrayToTree(list, key = 'permId', parentKey = 'parentId') {
  // 拿出所有父级类
  // const parentIds = new Set([...list.map(v => v.parentId)])

  // const topList = { children: [] }
  // topList.children.push(...recursive(0, list, key, parentKey))
  // return topList.children

  const tempMap = {}
  list.forEach(item => {
    let temp = tempMap[item[key]]
    temp = tempMap[item[key]] = { ...temp, ...item }
    const pid = item[parentKey] || 0
    const parent = tempMap[pid] = tempMap[pid] || {};
    (parent.children = parent.children || []).push(temp)
  })
  return tempMap[0].children
}

function recursive(parentId, list, key = 'permId', parentKey = 'parentId') {
  const filter = list.filter(item => item[parentKey] === parentId)
  for (let i = 0; i < filter.length; i++) {
    filter[i].children.push(...recursive(filter[i][key], list, key, parentKey))
  }
  return filter
}
