乐趣区

关于javascript:js根据某一个值或者某一个数组过滤出对象数组中符合条件的集合

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}]
退出移动版