使用Lodash查找对象的数组是否具有来自另一个数组的元素

时间:2019-04-02 11:03:47

标签: javascript arrays lodash

有一系列产品,每个产品的结构如下:

{
  id: 1,
  name: "product 1",
  materials: [
    {
      id: 1,
      name: "material 1"
    },
    {
      id: 2,
      name: "material 2"
    },
    {
      id: 3,
      name: "material 3"
    }
  ]
}

每种产品的阵列都包含不同数量的材料。

还有一组材料ID,例如[1, 4, 7, 2, 5]

如何过滤产品阵列以仅保留物料ID在物料ID阵列中的产品?

4 个答案:

答案 0 :(得分:1)

尝试

products.filter(p=> p.materials.some(m=> materialsIds.includes(m.id)));

let materialsIds = [1, 4, 7, 2, 5];
let products = [
{ id: 1, name: "product 1", materials: [{id: 1,name: "material 1"},{id: 2,name: "material 2"},{id: 3, name: "material 3"}]},

{ id: 2, name: "product 2", materials: [{id: 2, name: "material 2"}]},

{ id: 3, name: "product 3", materials: [{id: 3, name: "material 3"}]},

]

let r = products.filter(p=> p.materials.some(m=> materialsIds.includes(m.id)));

console.log('Filtered products ids', r.map(p=>p.id));
console.log('Filtered products', JSON.stringify(r));

答案 1 :(得分:0)

您可以使用filter() some()includes()

  • 在主要产品系列上使用filter()
  • 然后在对象some()上使用materials
  • 检查mats(材料ID)是否包含id的材料

let arr = [{ id: 1, name: "product 1", materials: [ { id: 1, name: "material 1" }, { id: 2, name: "material 2" }, { id: 3, name: "material 3" } ] },{ id: 2, name: "product 2", materials: [ { id: 11, name: "material 11" }, { id: 22, name: "material 22" }, { id: 33, name: "material 33" } ] } 

]
let mats = [1,5,6];

let res = arr.filter(x => x.materials.some(z=> mats.includes(z.id)));

console.log(res)

答案 2 :(得分:0)

您可以这样做:

import {intersection} from 'lodash'
const products = [...] 
const materialIds = [1,4,7,2,5]

// some function use es5+
const targetProducts = products.filter(p => intersection(p.materials.map(m => m.id), materialIds).length)

// use lodash only  import {filter, map, intersection} from 'lodash'
const targetProducts = filter(products, p => intersection(map(p.materials, 'id'), materialIds).length)

答案 3 :(得分:0)

const products = [
      {
        id: 1,
        name: 'product 1',
        materials: [
          {
            id: 1,
            name: 'material 1'
          },
          {
            id: 7,
            name: 'material 7'
          },
          {
            id: 5,
            name: 'material 5'
          }
        ]
      },
      {
        id: 2,
        name: 'product 2',
        materials: [
          {
            id: 1,
            name: 'material 1'
          },
          {
            id: 2,
            name: 'material 2'
          },
          {
            id: 3,
            name: 'material 3'
          }
        ]
      }
    ];
    const materials = [3, 4];
    console.log(
      products.filter(product => {
        for (let i = 0; i < product.materials.length; i++) {
          if (materials.includes(product.materials[i].id)) return true;
        }
      })
    );

我会这样:

 products.filter(product => {
    for (let i = 0; i < product.materials.length; i++) {
      if (materials.includes(product.materials[i].id)) return true;
    }
  })