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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理