当前位置: 移动技术网 > IT编程>开发语言>JavaScript > (ES6)数据处理常用工具方法收集(更新状态: on)

(ES6)数据处理常用工具方法收集(更新状态: on)

2019年09月26日  | 移动技术网IT编程  | 我要评论

 

1. 扁平数组转成tree结构(来源: stackoverflow的印度老哥写的)

 1 // data set
 2 // one top level comment 
 3 var comments = [{
 4     id: 1,
 5     parent_id: null
 6 }, {
 7     id: 2,
 8     parent_id: 1
 9 }, {
10     id: 3,
11     parent_id: 1
12 }, {
13     id: 4,
14     parent_id: 2
15 }, {
16     id: 5,
17     parent_id: 4
18 }];
19 
20 const nest = (items, id = null, link = 'parent_id') =>
21   items
22     .filter(item => item[link] === id)
23     .map(item => ({ ...item, children: nest(items, item.id) }));
24 
25 nest(comments);

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网