2

I have one array of objects "cars"

let cars = [
{id: 1, name: 'Mercedes', year: '2015'},
{id: 2, name: 'Mercedes', year: '2000'},
{id: 3, name: 'BMW', year: '2010'},
{id: 4, name: 'BMW', year: '2004'},
{id: 4, name: 'BMW', year: '2004'},
{id: 4, name: 'BMW', year: '2004'},
{id: 4, name: 'BMW', year: '2004'},
{id: 4, name: 'BMW', year: '2004'},
{id: 5, name: 'Volvo', year: '2012'},
{id: 6, name: 'Volvo', year: '2014'},
{id: 7, name: 'Volvo', year: '2010'},
{id: 8, name: 'Toyota', year: '2012'},
{id: 8, name: 'Jeep', year: '2011'},
];

and one object which contains the quantity of cars

let obj = {
BMW: 6,
Jeep: 1,
Mercedes: 2,
Toyota: 1,
Volvo: 3,
}

I need to create new array from cars which will have only one copy of object in it and add the "quantity" property to appropriate object. How can I achieve this?

expected output.

newArray = [
    {id: 1, name: 'Mercedes', year: '2015', quantity: 2},
    {id: 3, name: 'BMW', year: '2010', quantity: 6},
    {id: 5, name: 'Volvo', year: '2012', quantity: 3},
    {id: 8, name: 'Toyota', year: '2012', quantity: 1},
    {id: 8, name: 'Jeep', year: '2011', quantity: 1},
]
8
  • 1
    what do you mean by one copy of object in it ? Commented Jun 18, 2020 at 14:05
  • can you give an example of the output you expect? Currently, it's unclear whether you want unique objects by id or by name Commented Jun 18, 2020 at 14:06
  • I mean only one item with the same name property Commented Jun 18, 2020 at 14:06
  • 1
    what about id and year in the new objects? Commented Jun 18, 2020 at 14:10
  • 1
    id and year they are not important in this case. I edited the code with expected example Commented Jun 18, 2020 at 14:11

5 Answers 5

2

You could count directly by using the array with a hash table.

let cars = [{ id: 1, name: 'Mercedes', year: '2015' }, { id: 2, name: 'Mercedes', year: '2000' }, { id: 3, name: 'BMW', year: '2010' }, { id: 4, name: 'BMW', year: '2004' }, { id: 4, name: 'BMW', year: '2004' }, { id: 4, name: 'BMW', year: '2004' }, { id: 4, name: 'BMW', year: '2004' }, { id: 4, name: 'BMW', year: '2004' }, { id: 5, name: 'Volvo', year: '2012' }, { id: 6, name: 'Volvo', year: '2014' }, { id: 7, name: 'Volvo', year: '2010' }, { id: 8, name: 'Toyota', year: '2012' }, { id: 8, name: 'Jeep', year: '2011' }],
    result = Object.values(cars.reduce((r, o) => {
        r[o.name] = r[o.name] || { ... o, quantity: 0 };
        r[o.name].quantity++;
        return r;
    }, []));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

1

Creating a new array with all the items from cars object and the count from obj without any duplication.

let cars = [{"id":1,"name":"Mercedes","year":"2015"},{"id":2,"name":"Mercedes","year":"2000"},{"id":3,"name":"BMW","year":"2010"},{"id":4,"name":"BMW","year":"2004"},{"id":4,"name":"BMW","year":"2004"},{"id":4,"name":"BMW","year":"2004"},{"id":4,"name":"BMW","year":"2004"},{"id":4,"name":"BMW","year":"2004"},{"id":5,"name":"Volvo","year":"2012"},{"id":6,"name":"Volvo","year":"2014"},{"id":7,"name":"Volvo","year":"2010"},{"id":8,"name":"Toyota","year":"2012"},{"id":8,"name":"Jeep","year":"2011"}];
let obj = {BMW: 6,Jeep: 1,Mercedes: 2,Toyota: 1,Volvo: 3}
let newObj = [];
let duplicateCheck = []
cars.forEach(function(item) {
  if (!duplicateCheck.includes(item.name)) {
    duplicateCheck.push(item.name);
    item.quantity = obj[item.name];
    newObj.push(item);
  }
});
console.log(newObj);

Comments

1

In order to come up with the new array, it needs to process by two steps:

  1. Keep the first car with unique brand from cars array
  2. Attach quantity to the car from obj

let cars=[
  {id:1,name:'Mercedes',year:'2015'},
  {id:2,name:'Mercedes',year:'2000'},
  {id:3,name:'BMW',year:'2010'},
  {id:4,name:'BMW',year:'2004'},
  {id:4,name:'BMW',year:'2004'},
  {id:4,name:'BMW',year:'2004'},
  {id:4,name:'BMW',year:'2004'},
  {id:4,name:'BMW',year:'2004'},
  {id:5,name:'Volvo',year:'2012'},
  {id:6,name:'Volvo',year:'2014'},
  {id:7,name:'Volvo',year:'2010'},
  {id:8,name:'Toyota',year:'2012'},
  {id:8,name:'Jeep',year:'2011'}
];
let obj = {
  BMW:6,
  Jeep:1,
  Mercedes:2,
  Toyota:1,
  Volvo:3,
};

/* The first step:
you can use .filter to 
only return the car you are looking for.
*/
let uniqueCarBrand = [];
let newArray = cars.filter((car) => {
  /* Check if car name already in the newArray */
  if (uniqueCarBrand.indexOf(car.name) >= 0) return false;
  uniqueCarBrand.push(car.name);
  return true;
});

/* Second step:
you will need to attach your quantity to
the array of cars you just created.
*/
let result = newArray.map((car) => {
  /* Check if obj has brand name in it, if not, return 0; */
  car.quantity = obj[car.name] || 0;
  return car;
});

console.log(result);

Comments

0

You could use two loops to match up the objects from both of your arrays:

let output = [];
// For each key in your quantity object
for (let model in obj) {
    // For each car object in your cars array
    for (let car of cars) {
        // This will match the first car in your cars array
        if (car.name === model) {
            // Add quantity to your car object and add it to your result set
            car.quantity = obj[model];
            output.push(car);
            // This break will continue to the next model car to ensure you only have one result per model
            break;
        }
    }
}

1 Comment

your code does not give the desired result, the output array is not sorted by id element
0

Using ES6 modules

let cars = [
  {id: 1, name: 'Mercedes', year: '2015'},
  {id: 2, name: 'Mercedes', year: '2000'},
  {id: 3, name: 'BMW', year: '2010'},
  {id: 4, name: 'BMW', year: '2004'},
  {id: 4, name: 'BMW', year: '2004'},
  {id: 4, name: 'BMW', year: '2004'},
  {id: 4, name: 'BMW', year: '2004'},
  {id: 4, name: 'BMW', year: '2004'},
  {id: 5, name: 'Volvo', year: '2012'},
  {id: 6, name: 'Volvo', year: '2014'},
  {id: 7, name: 'Volvo', year: '2010'},
  {id: 8, name: 'Toyota', year: '2012'},
  {id: 8, name: 'Jeep', year: '2011'},
];
let obj = {
  BMW: 6,
  Jeep: 1,
  Mercedes: 2,
  Toyota: 1,
  Volvo: 3,
}  

let filteredCar = cars.filter((curr, index) => {
  let _cars = JSON.stringify(curr);
  return index === cars.findIndex(obj => {
    return JSON.stringify(obj) === _cars;
  })
}).map(currCar => {
  currCar.quantity = obj[currCar.name];
                   return currCar});

console.log(filteredCar);

Comments

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.