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


 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' } ]


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM