filter实现数据过滤
data1数组内对象的属性过滤,然后求出count
let data1 = [ { count: 1, remove: true }, { count: 3, add: true }, { count: 1, remove: false }, { count: 3, add: false }, { count: 3 } ]; // filter方法 let b = 0; data1.filter(function (data1) { if (!data1.remove || data1.add === true) { b += data1.count } }) console.log(b);
map实现数据过滤
data1数组内对象的属性过滤,然后求出count
// map方法 let data1 = [ { count: 1, remove: true }, { count: 3, add: true }, { count: 1, remove: false }, { count: 3, add: false }, { count: 3 } ]; let a = 0; data1.map(item => { if (!item.remove || item.add === true) { a += item.count } }) console.log(a);
JS基础:数组内对象的过滤(filter、map)
最新推荐文章于 2024-09-11 05:44:14 发布