在javascript中过滤对象数组

时间:2021-07-23 22:57:47

I have a data structure that looks like this:

我有一个如下所示的数据结构:

const carsData = [
  {
    name: "Cars",
    collection: [
      { year: 2011, model: "B", price: 4400 },
      { year: 2015, model: "A", price: 32000 },
      { year: 2016, model: "B", price: 15500 }
    ]
  },
  {
    name: "Trucks",
    collection: [
      { year: 2014, model: "D", price: 18000 },
      { year: 2013, model: "E", price: 5200 }
    ]
  },
  {
    name: "Convertibles",
    collection: [
      { year: 2009, model: "F", price: 20000 },
      { year: 2010, model: "G", price: 8000 },
      { year: 2012, model: "H", price: 12500 },
      { year: 2017, model: "M", price: 80000 }
    ]
  }
];

and want to return a new array let's say const newCarsData(see below) where collection consists of only objects with year higher than 2013, so it will look like this:

并且想要返回一个新数组让我们说const newCarsData(见下文)其中collection只包含年份高于2013的对象,所以它看起来像这样:

const newCarsData = [
  {
    name: "Cars",
    collection:[
      { year: 2015, model: "A", price: 32000 },
      { year: 2016, model: "B", price: 15500 }
    ]
  },
  {
    name: "Trucks",
    collection: [
      { year: 2014, model: "D", price: 18000 }
    ]
  },
  {
    name: "Convertibles",
    collection: [
      { year: 2017, model: "M", price: 80000 }
    ]
  }
];

I tried filter method collection.filter(x => x.year > 2013) inside of for loop, but couldn't make it work. At the end my code looked like this

我在for循环中尝试了filter方法collection.filter(x => x.year> 2013),但无法使其工作。最后,我的代码看起来像这样

const newCarsData = getNewData(carsData);
let arr = [];
function getNewData(somedata) {
  for (let i = 0; i < somedata.length; i++) {
    // console.log(somedata[i].collection);
    for (let j = 0; j < somedata[i].collection.length; j++) {
      let arr.push(somedata[i].collection[j]);
      // console.log(somedata[i].collection[j]);
    }
    // return somedata[i].collection.filter(x => x.year > 2013);
  }
  return arr.filter(x => x.year > 2013);
}

3 个解决方案

#1


3  

Since the collection is in another array inside the array items, you can't directly use filter. You can use map first then use filter.

由于集合在数组项内的另一个数组中,因此无法直接使用过滤器。您可以先使用地图然后使用过滤器。

const carsData=[{name:"Cars",collection:[{year:2011,model:"B",price:4400},{year:2015,model:"A",price:32000},{year:2016,model:"B",price:15500}]},{name:"Trucks",collection:[{year:2014,model:"D",price:18000},{year:2013,model:"E",price:5200}]},{name:"Convertibles",collection:[{year:2009,model:"F",price:20000},{year:2010,model:"G",price:8000},{year:2012,model:"H",price:12500},{year:2017,model:"M",price:80000}]}]

const filteredCarData = carsData.map(carType => {
    return {
        ...carType,
        collection: carType.collection.filter(car => car.year>2013)
    }
})

console.log(JSON.stringify(filteredCarData))
    

The ...carType notation collects the object properties in the new mapped object. If you have no other properties than name, you can instead do

... carType表示法收集新映射对象中的对象属性。如果您没有名称以外的其他属性,则可以这样做

const filteredCarData = carsData.map(carType => {
    return {
        name: carType.name,
        collection: carType.collection.filter(car => car.year>2013)
    }
})

#2


0  

You'll have to update your collection:

您必须更新您的收藏:

carsData.forEach(function(carData){
    carData.collection = carData.collection.filter(x => x.year > 2013);
});

#3


0  

One way to do it can be using reduce.

一种方法是使用reduce。

var res = carsData.reduce((acc, value) => {
    let data = { name: value.name, collections: value.collection.filter(v => v.year > 2013 )}
    return acc.concat(data)
}, [])

You can actually, replace the reduce with a map:

实际上,您可以使用地图替换reduce:

carsData.map(value => {
  return { name: value.name, collections: value.collection.filter(v => v.year > 2013 )}
})

#1


3  

Since the collection is in another array inside the array items, you can't directly use filter. You can use map first then use filter.

由于集合在数组项内的另一个数组中,因此无法直接使用过滤器。您可以先使用地图然后使用过滤器。

const carsData=[{name:"Cars",collection:[{year:2011,model:"B",price:4400},{year:2015,model:"A",price:32000},{year:2016,model:"B",price:15500}]},{name:"Trucks",collection:[{year:2014,model:"D",price:18000},{year:2013,model:"E",price:5200}]},{name:"Convertibles",collection:[{year:2009,model:"F",price:20000},{year:2010,model:"G",price:8000},{year:2012,model:"H",price:12500},{year:2017,model:"M",price:80000}]}]

const filteredCarData = carsData.map(carType => {
    return {
        ...carType,
        collection: carType.collection.filter(car => car.year>2013)
    }
})

console.log(JSON.stringify(filteredCarData))
    

The ...carType notation collects the object properties in the new mapped object. If you have no other properties than name, you can instead do

... carType表示法收集新映射对象中的对象属性。如果您没有名称以外的其他属性,则可以这样做

const filteredCarData = carsData.map(carType => {
    return {
        name: carType.name,
        collection: carType.collection.filter(car => car.year>2013)
    }
})

#2


0  

You'll have to update your collection:

您必须更新您的收藏:

carsData.forEach(function(carData){
    carData.collection = carData.collection.filter(x => x.year > 2013);
});

#3


0  

One way to do it can be using reduce.

一种方法是使用reduce。

var res = carsData.reduce((acc, value) => {
    let data = { name: value.name, collections: value.collection.filter(v => v.year > 2013 )}
    return acc.concat(data)
}, [])

You can actually, replace the reduce with a map:

实际上,您可以使用地图替换reduce:

carsData.map(value => {
  return { name: value.name, collections: value.collection.filter(v => v.year > 2013 )}
})