1

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 = [
  {
    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

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);
}
5
  • Where is the code? Commented Jan 30, 2018 at 3:22
  • 1
    Welcome to Stack Overflow. This is not a code/SQL/regex writing service, where you post a list of your requirements and language of choice and a code monkey churns out code for you. We're more than happy to help, but we expect you to make an effort to solve the problem yourself first. Once you've done so, you can explain the problem you're having, include the relevant portions of your work, and ask a specific question, and we'll try to help. Good luck. Commented Jan 30, 2018 at 3:25
  • where you post a list of your requirements and language of choice and a code monkey churns out code for you lol . nice. Commented Jan 30, 2018 at 3:29
  • ok, thanks. Will consider and will update the question with my unsolved code snippet Commented Jan 30, 2018 at 3:29
  • @d_oram two things, when nothing fits your bill, try reduce, second reactivex.io/learnrx Commented Jan 30, 2018 at 3:33

3 Answers 3

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

const filteredCarData = carsData.map(carType => {
    return {
        name: carType.name,
        collection: carType.collection.filter(car => car.year>2013)
    }
})
Sign up to request clarification or add additional context in comments.

Comments

0

You'll have to update your collection:

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

Comments

0

One way to do it can be using 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:

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

2 Comments

But please go throught all the exercises in reactivex.io/learnrx
I usually use higher order functions but with nested data structure like this I wasn't able to come up with right logic, so i reached for loops and wrote some silly code as you see. Will do!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.