关于javascript:对象数组-转为-树形结构

38次阅读

共计 440 个字符,预计需要花费 2 分钟才能阅读完成。

// ****** 对象数组 ==> 树
__arrToTree(arr, pid = "superiorId", id = "id") {let tree = [];
 _.remove(arr, item => {if (item[pid] === "0"){tree.push(item)
 return true
 }
 return false
 })
 // 递归
 const getChildren = (arr, tree, pid, id) => {if (!arr || !tree)return;
 tree.forEach(i => {
 _.remove(arr, j => {if (j[pid] === i[id]) {i.children.push(j)
 return true
 }
 return false
 })
 if (i.children.length === 0) {delete i.children;}else {getChildren(arr, i.children, pid, id)
 } }) } getChildren(arr, tree, pid, id)
 return tree
}
// ******

正文完
 0