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

// ****** 对象数组 ==> 树
__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
}
// ******

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理