设为首页 加入收藏

TOP

(ES6)数据处理常用工具方法收集(更新状态: on)
2019-09-26 18:14:44 】 浏览:38
Tags:ES6 数据处理 常用工具 方法 收集 更新 状态

 

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);

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇微信小程序与用户交互 下一篇js验证小数或者数字

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目