实现对象数组的扁平化处理?

 1 const data = [{
 2    name: "撤回",
 3    type: "withdraw"
 4 }, {
 5    name: "...",
 6    type: "other",
 7    children: [{
 8       name: "会签",
 9       type: "sign"
10    }]
11 }];
12 
13 const flat = data.reduce((pre, cur) => {
14    return cur.children ? pre.concat(cur.children) : pre.concat(cur);
15 }, []);

打印结果

[ { name: '撤回', type: 'withdraw' },
{ name: '会签', type: 'sign' } ]
posted @ 2020-11-05 17:20  程序員劝退师  阅读(1863)  评论(2编辑  收藏  举报