1

I'm stuck on this one thing trying to take top 3 manufacturers by numberOfCars, and, to do the same with cars: [], take top 3 by numberoOfCars

audi: {
  cars: [], 
  collectionName: '',
  numberOfCars: 0
}

I can do the first level with lodash like

_.take(_.orderBy(modelData, 'numberoOfCars', 'desc'), 3)

but I'm lost on doing the same on cars array as mentioned.

const mock = []

for (let i = 0; i < 140; i++) {
  let manufacturerName
  let name

  if (i < 20) {
    manufacturerName = 'Audi'
    name = 'A6'
  } else if (i > 19 && i < 40) {
    manufacturerName = 'BMW'
    name = '420 GC'
  } else if (i > 19 && i < 40) {
    manufacturerName = 'Mercedes'
    name = 'AMG'
  } else if (i > 39 && i < 60) {
    manufacturerName = 'Mazda'
    name = '6'
  } else if (i > 59 && i < 80) {
    manufacturerName = 'Volvo'
    name = 'V90'
  } else if (i > 79 && i < 100) {
    manufacturerName = 'Renault'
    name = 'Model'
  } else if (i > 99 && i < 120) {
    manufacturerName = 'Lamborghini'
    name = 'Aventador'
  } else if (i > 119 && i < 140) {
    manufacturerName = 'Volkswagen'
    name = 'Golf'
  }

  mock.push({
    id: i,
    name: name,
    displayName: 'display-name ' + Math.floor(Math.random() * 10),
    manufacturer: manufacturerName,
    numberoOfCars: Math.floor(Math.random() * 100000),
  })
}

const dataModel = mock.reduce((accumulator, currentValue) => {
  const key = currentValue.manufacturer

  if (accumulator[key] === undefined) {
    accumulator[key] = {
      collectionName: '',
      numberoOfCars: 0,
      cars: []
    }
  }

  if (accumulator[key].collectionName === '') {
    accumulator[key].collectionName = currentValue.manufacturer
  }

  if (currentValue.numberoOfCars !== undefined) {
    accumulator[key].numberoOfCars += currentValue.numberoOfCars
  }

  if (currentValue.numberoOfCars !== undefined) {
    accumulator[key].cars.push({
      name: currentValue.name,
      numberOfCars: currentValue.numberoOfCars
    })
  }

  return accumulator
}, {})

console.log(dataModel)
Dejan.S
  • 18,571
  • 22
  • 69
  • 112

2 Answers2

0

I guess it should be something like this:

const cars = Object.keys(dataModel).reduce((acc, item) => {
  const collection = dataModel[item]
  return acc.concat(collection.cars)
}, []);

const top3cars = _.take(_.orderBy(cars, 'numberOfCars', 'desc'), 3);
Hero Qu
  • 911
  • 9
  • 10
0

Iterate over the keys in the outer object, order those keys by the number of cars associated with that key in the outer then pick the first 3 keys.

Then, reduce each key into an object that clones the value associated with that key and orders the cars in that value and takes the first 3:

var filtered = _.chain(dataModel)
    .keys()
    .orderBy(k=>dataModel[k].numberOfCars, 'desc')
    .take(3)
    .reduce((coll,k)=>{
        coll[k] = _.clone(dataModel[k]);
        coll[k].cars = _.chain(coll[k].cars)
            .orderBy('numberOfCars', 'desc')
            .take(3)
            .value();
        return coll;
    }, {})
    .value();

Example on jsfiddle

Credit to this answer by VLAZ for help on how to filter an object's items by an ordering over its values.

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
  • Thanks. I'm trying to understand one thing, is there a way to do this without the second reduce, like why is that necessary? Dont take it the wrong way, this is my learning process :) – Dejan.S Oct 30 '19 at 06:56
  • @Dejan.S Second `reduce`? I'm only use one `reduce`. – Ruzihm Oct 30 '19 at 17:35