过滤javascript中的对象数组

过滤javascript中的对象数组,javascript,arrays,for-loop,filter,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",

我的数据结构如下所示:

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 = [
  {
    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 }
    ]
  }
];
并且想要返回一个新数组,比如说
const newCarsData
(见下文),其中集合只包含年份高于2013年的对象,因此它将如下所示:

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 = [
  {
    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循环内部尝试了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);
}
const newCarsData=getNewData(carsData);
设arr=[];
函数getNewData(somedata){
for(设i=0;ix.year>2013);
}
返回arr.filter(x=>x.year>2013);
}

您必须更新您的收藏:

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

一种方法是使用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 )}
})

由于集合位于数组项内的另一个数组中,因此不能直接使用
过滤器
。您可以先使用
map
,然后使用
filter

const carsData=[{name:“Cars”,集合:[{year:2011,model:B,price:4400},{year:2015,model:A,price:32000},{year:2016,model:B,price:15500}},{name:“Trucks”,集合:[{year:2014,model:D,price:18000},{year:2013,model:E,price:5200},{name:敞篷车,集合:[{year:2009,model:F,price:20000},{year:2010,model:8000},{年份:2012,型号:“H”,价格:12500},{年份:2017,型号:“M”,价格:80000}]
常量filteredCarData=carsData.map(carType=>{
返回{
…卡式,
集合:carType.collection.filter(car=>car.year>2013)
}
})
log(JSON.stringify(filteredCarData))

代码在哪里?欢迎使用Stack Overflow。这不是一个代码/SQL/regex编写服务,你可以在这里发布你的要求和选择的语言列表,然后由一个代码猴子为你编写代码。我们非常乐意提供帮助,但我们希望你首先自己努力解决问题。一旦你这样做了,你就无法解释了你所面临的问题,包括你的工作的相关部分,并问一个具体的问题,我们会尽力帮助。好运。<代码>你在其中列出你的需求和语言的选择列表和一个代码猴子为你输出代码< /COD> LOL.NICE.OK,谢谢。我会考虑并用我的未解决的代码片段来更新这个问题。@d_oram两件事,当没有任何东西符合您的要求时,尝试减少,第二,但请完成中的所有练习。我通常使用高阶函数,但对于这样的嵌套数据结构,我无法想出正确的逻辑,所以我尝试循环并编写一些愚蠢的代码,如您所见。可以!