为了账号安全,请及时绑定邮箱和手机立即绑定

怎么过滤数组

怎么过滤数组

慕后森 2019-05-10 15:34:25
刚开始的两个数组是这样的,b数组是在a数组的基础上加了一个字段a=[{label:'周一',organizeId:'1001'},{label:'周二',organizeId:'1002'},{label:'周三',organizeId:'1003'},{label:'周四',organizeId:'1004'},{label:'周五',organizeId:'1005'}]b=[{children:[{label:'周一',organizeId:'1001',subValue:'100'},{label:'周二',organizeId:'1002',subValue:'200'},{label:'周三',organizeId:'1003',subValue:'1000'},{label:'周四',organizeId:'1004',subValue:'800'},{label:'周五',organizeId:'1005',subValue:'600'}]},{children:[{label:'周一',organizeId:'1001',subValue:'100'},{label:'周二',organizeId:'1002',subValue:'200'},{label:'周三',organizeId:'1003',subValue:'1000'},{label:'周四',organizeId:'1004',subValue:'800'},{label:'周五',organizeId:'1005',subValue:'600'}]}]当a的数据变化之后a=[{label:'周一',organizeId:'1001'},{label:'周二',organizeId:'1002'},{label:'周六',organizeId:'1006'}]我想得到这样的数组,之前有的数据不变,没有的比如周六,就加上并将subValue值设为0,去掉的,比如周三,周四,周五,新的数组里的children里也去掉b=[{children:[{label:'周一',organizeId:'1001',subValue:'100'},{label:'周二',organizeId:'1002',subValue:'200'},{label:'周六',organizeId:'1006',subValue:'0'}]},{children:[{label:'周一',organizeId:'1001',subValue:'100'},{label:'周二',organizeId:'1002',subValue:'200'},{label:'周六',organizeId:'1006',subValue:'0'}]}]
查看完整描述

2 回答

?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

constnewA=[
{
label:'周一',
organizeId:'1001',
},
{
label:'周二',
organizeId:'1002',
},
{
label:'周六',
organizeId:'1006',
},
]
//找出新增的项
constaddArr=newA.filter(d=>!a.find(d1=>d1.organizeId===d.organizeId))
b=b.map(item=>{
letchild=[...item.children]
//过滤没有的项
child=child.filter(c=>newA.find(n=>n.organizeId===c.organizeId))
//添加新增的项
addArr.forEach(d=>child.push({...d,subValue:0}))
return{child}
})
                            
查看完整回答
反对 回复 2019-05-10
?
芜湖不芜

TA贡献1796条经验 获得超7个赞

b.forEach(n=>{
constchildren=n.children;
constnewChildren=children.filter(o=>a.some(i=>i.label===o.label));
constoldChildren=a.filter(o=>children.every(i=>i.label!==o.label)).map(o=>{
o.subValue="0";
returno;
});
n.children=newChildren.concat(oldChildren);
});
console.log(b);
                            
查看完整回答
反对 回复 2019-05-10
  • 2 回答
  • 0 关注
  • 599 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信