在javascript中过滤对象数组

时间:2018-01-30 03:20:20

标签: javascript arrays for-loop 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 newCarsData(见下文),其中集合仅包含年份高于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 }
    ]
  }
];

我在for循环中尝试了过滤方法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 个答案:

答案 0 :(得分:3)

由于集合位于数组项中的另一个数组中,因此您无法直接使用filter。您可以先使用map,然后使用filter

&#13;
&#13;
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))
    
&#13;
&#13;
&#13;

...carType表示法收集新映射对象中的对象属性。如果您没有name之外的其他属性,则可以改为

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

答案 1 :(得分:0)

您必须更新您的收藏:

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

答案 2 :(得分:0)

一种方法是使用reduce。

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

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

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