共计 468 个字符,预计需要花费 2 分钟才能阅读完成。
js 依据某一个值或者某一个数组过滤出对象数组中符合条件的汇合,利用 filter 和 includes:
案例 1:
const target = [{a: [1, 2, 3]}, {a: [11, 22, 31]}, {a: [9, 7, 3]}];
const tempSpecId = 3;
const selectedArray = target.filter((item) => {return item.a.includes(tempSpecId)
});
console.log(selectedArray, 'selectedArray-')
// [{a: [1, 2, 3]}, {a: [9, 7, 3]}]
案例 2:
const specIds = [1, 2, 3, 4];
const productList = [{a: 1}, {a: 22}, {a: 3}];
const selectedArray = productList.filter(item => {return !specIds.includes(item.a)
});
console.log(selectedArray, 'selectedArray')
// [{a: 22}]
正文完
发表至: javascript
2022-10-21