简体   繁体   English

如何过滤嵌套的对象数组?

[英]How to filter a nested array of objects?

I have an array of objects in which i am trying to filter based on a give condition.我有一个对象数组,我试图根据给定条件过滤其中的对象。 The array contains objects with company and products fields.该数组包含具有companyproducts字段的对象。

The filter term is productType: "Timespot" .过滤器术语是productType: "Timespot"

I would like to get the company and its products which only has the productType: "Timespot"我想获得只有productType: "Timespot"的公司及其产品

const dummyData = [
  {
    companyName: "Company A",
    products: [
      {
        productType: "Variabel pris",
      },
    ],
  },
  {
    companyName: "Company B",
    products: [
      {
        productType: "Fastpris",
      },
      {
        productType: "Fastpris",
      },
      {
        productType: "Fastpris",
      },
      {
        productType: "Timespot",
      },
    ],
  },
  {
    companyName: "Company C",
    products: [
      {
        productType: "Spotpris",
      },
      {
        productType: "Timespot",
      },
    ],
  },
];
const expectedResult = [
  {
    companyName: "Company B",
    products: [
      {
        productType: "Timespot",
      },
    ],
  },
  {
    companyName: "Company C",
    products: [
      {
        productType: "Timespot",
      },
    ],
  },
];

What i am trying:我正在尝试什么:

  dummyData.filter(({ products }) => {
     products.some(product => product.productType === "Timespot")
  });

To provide more context is that i want to get the company and its product which match the given condition.为了提供更多背景信息,我希望获得符合给定条件的公司及其产品。

I think the main problem with your approach is that the filter function will only filter the first array and not the nested one, in order to do what you desire I would use a reduce()我认为你的方法的主要问题是过滤器 function 只会过滤第一个数组而不是嵌套的数组,为了做你想做的事,我会使用reduce()

eg例如

dummyData.reduce((acc,cur)=> { 
    const filteredProducts = cur.products.filter((p)=> p.productType === "Timespot")

    if(filteredProducts.length){
        acc.push({...cur, products:filteredProducts})    
    }
    
    return acc

}, [])

I'd probably do something like this:我可能会做这样的事情:

function filterProductType(arr, prodType) {
  return arr.filter(obj => obj.products.some(type => type.productType === prodType));
}

console.log(filterProductType(dummyData, "Timespot"));

Returns:回报:


0: companyName: "Company B"
products: Array(4)
  0: {productType: 'Fastpris'}
  1: {productType: 'Fastpris'}
  2: {productType: 'Fastpris'}
  3: {productType: 'Timespot'}
length: 4

1: companyName: "Company C"
products: Array(2)
  0: {productType: 'Spotpris'}
  1: {productType: 'Timespot'}
length: 2

You can use array.filter on the main array, combined with array.find on the nested array of products.您可以在主数组上使用array.filter ,并在嵌套的产品数组上使用array.find

 const dummyData=[{companyName:"Company A",products:[{productType:"Variabel pris"}]},{companyName:"Company B",products:[{productType:"Fastpris"},{productType:"Fastpris"},{productType:"Fastpris"},{productType:"Timespot"}]},{companyName:"Company C",products:[{productType:"Spotpris"},{productType:"Timespot"}]}]; const filtered = dummyData.filter(({products}) => { return products.find(({productType}) => productType === 'Timespot'); }) console.log(filtered)

If you find that you also need to filter by some other productType elsewhere, you could make a more generic function to handle this.如果您发现您还需要在其他地方按其他一些productType进行过滤,您可以制作更通用的 function 来处理这个问题。

 const dummyData=[{companyName:"Company A",products:[{productType:"Variabel pris"}]},{companyName:"Company B",products:[{productType:"Fastpris"},{productType:"Fastpris"},{productType:"Fastpris"},{productType:"Timespot"}]},{companyName:"Company C",products:[{productType:"Spotpris"},{productType:"Timespot"}]}]; const companyByProductType = ({data, type}) => { return data.filter(({products}) => { return products.find(({productType}) => productType === type); }) } console.log(companyByProductType({data: dummyData, type: 'Timespot'}))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM