Skip to content

Commit

Permalink
Update 关系数组转成树形结构对象.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Rain120 authored Aug 21, 2018
1 parent ef9e860 commit ae27c17
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions 关系数组转成树形结构对象.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@ function treeObj(obj) {
});
return obj.filter(item => item.parent === null)[0]
}
function treeObj(obj) {
return obj.sort((a, b) => b.parent - a.parent).reduce((acc, cur) => (acc ? { ...cur, child: acc } : cur));
}
obj = treeObj(obj)
console.log(obj)

0 comments on commit ae27c17

Please sign in to comment.