将普通数组转换为tree数组

// 将普通数组转换为tree数组
export function transData (a, idStr, pidStr, chindrenStr) {
  const r = []
  const hash = {}
  const id = idStr
  const pid = pidStr
  const children = chindrenStr
  let i = 0
  let j = 0
  const len = a.length
  for (; i < len; i++) {
    hash[a[i][id]] = a[i]
  }
  for (; j < len; j++) {
    const aVal = a[j]
    const hashVP = hash[aVal[pid]]
    if (hashVP) {
      !hashVP[children] && (hashVP[children] = [])
      hashVP[children].push(aVal)
    } else {
      r.push(aVal)
    }
  }
  return r
}

使用例子:

this.navDataList:

 

 

this.navList = transData(JSON.parse(JSON.stringify(this.navDataList)), 'id', 'parentId', 'children')

第一个参数是要转换的数组,要转变为json字符串

第二个参数是数组里的id,用来区别每个元素

第三个参数是元素对应的父元素的id值

第四个参数是父元素中子元素的名称

 

posted @ 2020-07-13 14:43  zhaobao1830  阅读(1075)  评论(0编辑  收藏  举报