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