Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.3k views
in Technique[技术] by (71.8m points)

根据id数组,删除二维对象数组中的指定项

var arr = [1,4,5];

                var allArr = [
                    {'shop_name':'水果店','goods':[{id:1,name:"s"},{id:2,name:"ds"},{id:3,name:"gg"}]},
                    {'shop_name':'水果店1','goods':[{id:4,name:"s"},{id:5,name:"ds"},{id:6,name:"gg"}]},
                ]
                最终变成
                var allArr = [
                    {'shop_name':'水果店','goods':[{id:2,name:"ds"},{id:3,name:"gg"}]},
                    {'shop_name':'水果店1','goods':[{id:6,name:"gg"}]},
                ]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
allArr.reduce((res,item) => (res.push({...item, goods: item.goods.filter(v => !arr.includes(v.id))}), res), []);

[ps: 一个数组就难倒这么多人吗?都已经看到和回答过N遍这种问题了]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...