You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
function arrToTre(params) {
const result = params.map(item => {
if (!item.children) {
item.children = []
}
const parent = findParent(item,params)
parent && parent.children.push(item)
return item
})
return result[lowIndex(params)]
}
function findParent(obj,arr) {
let result = null
arr.map(item => {
if (item.id === obj.parentId) {
item.children ? '' : item.children = []
result = item
}
})
return result
}
function lowIndex(arr) {
let ind = Infinity
let id = Infinity
arr.map((item,index) => {
if (item.id < id){
id = item.id
ind=index
}
})
return ind
}
const result = arrToTre(input)
Activity
[-]### 第 29 题:手写数组转树[/-][+]第 29 题:手写数组转树[/+]Genzhen commentedon Jun 23, 2020
Genzhen commentedon Jun 23, 2020
例如
[{id:1, parentId: 0}, {id:2, parentId:1},{id:3, parentId:1}]
把这个数组从顶级分类递归查找子分类,最终构建一个树状数组。结果输出如下
[{id:1, parentId: 0,children:[{id:2, parentId:1},{id:3, parentId:1}]}]
parentId为0 的是根节点
代码实现
Genzhen commentedon Jun 23, 2020
数组转树的其中一种,排序数组转二叉搜索树
523451928 commentedon Jul 15, 2020
fengmiaosen commentedon Jul 20, 2020
GolderBrother commentedon Jul 21, 2020
wang2498 commentedon Aug 3, 2020
xiaobei07 commentedon Aug 5, 2020
function arrToTre(params) {
const result = params.map(item => {
if (!item.children) {
item.children = []
}
const parent = findParent(item,params)
parent && parent.children.push(item)
return item
})
return result[lowIndex(params)]
}
function findParent(obj,arr) {
let result = null
arr.map(item => {
if (item.id === obj.parentId) {
item.children ? '' : item.children = []
result = item
}
})
return result
}
function lowIndex(arr) {
let ind = Infinity
let id = Infinity
arr.map((item,index) => {
if (item.id < id){
id = item.id
ind=index
}
})
return ind
}
const result = arrToTre(input)
v1nkon commentedon Oct 19, 2020
NameWjp commentedon Oct 29, 2020
8 remaining items
fanerge commentedon May 20, 2021
什么意思呢?子节点的先后顺讯吗?可以看看比较好的解法
Luoyuda commentedon Jun 10, 2021
wjiantao commentedon Jul 11, 2021
RaymoneLin21 commentedon Aug 12, 2021
function toTree(data) {
var result = [];
var map = {};
data.forEach((item) => {
map[item.id] = item;
});
data.forEach((item) => {
var parent = map[item.parentId];
if (parent) {
(parent.children || (parent.children = [])).push(item);
} else {
result.push(item);
}
});
return result;
}
console.log(toTree(input))
guanghechen commentedon Sep 5, 2021
@laihaoshan 先将数据转成树上的节点,然后根据
parentId
连边,parentId
为null
的那个就是根节点。1uckyneo commentedon Sep 29, 2021
safarishi commentedon Oct 21, 2021
zizxzy commentedon Nov 5, 2021
SnailOwO commentedon Dec 28, 2021
因为js数组是引用的
zhou-pro commentedon Aug 24, 2022
lang711 commentedon Aug 27, 2022
Kisthanny commentedon Mar 21, 2024
HQHC commentedon May 7, 2024
function arrayToTree(arr, id = null) {
return arr
.filter(item => item.parentId === id)
.map(item => ({ ...item, children: arrayToTree(arr, item.id) }));
}
chamsonxie commentedon Feb 13, 2025
利用引用的特点 无递归 只遍历一边
coderlyu commentedon Jun 15, 2025