-4

I need merge two arrays of objects, and no duplicates, with es6 syntax. In my task few more keys in objects.

But I think this is not problem. In array2 always objects that are contained inside array1

Here is the array

array1 = [
   {name: 'one', count: 0}, 
   {name: 'two', count: 0}, 
   {name: 'three', count: 0},
   {name: 'four', count: 0} 
]

array2 = [
   {name: 'two', count: 4},
   {name: 'four', count: 2}
]

I need get this:

array3 = [
  {name: 'one', count: 0},
  {name: 'two', count: 4},
  {name: 'three', count: 0},
  {name: 'four', count: 2}
]
Ritul Lakhtariya
  • 362
  • 1
  • 16
Remi Fokz
  • 121
  • 1
  • 1
  • 8
  • 1
    We can guide you or unblock you, but you have to make an attempt first. Please show what you've tried so far. – Jeremy Thille Jan 21 '19 at 13:28
  • [enter link description here](https://stackoverflow.com/questions/7146217/merge-2-arrays-of-objects) Try solutions on this link. – monofal Jan 21 '19 at 13:30
  • 2
    if in array1 you have an tem with count 1 for example, and in the array 2 you have the same item with count 2, what will be the count value of the item included in the final array? – quirimmo Jan 21 '19 at 13:33
  • Possible duplicate of [Merge 2 arrays of objects](https://stackoverflow.com/questions/7146217/merge-2-arrays-of-objects) – Ritul Lakhtariya Jan 21 '19 at 13:33

3 Answers3

1

const array1 = [
  {name: 'one', count: 0}, 
  {name: 'two', count: 0}, 
  {name: 'three', count: 0},
  {name: 'four', count: 0} 
]
const array2 = [
  {name: 'two', count: 4},
  {name: 'four', count: 2}
]

const result = array1.map(a => {
  const b = array2.find(b => b.name === a.name);
  if (b) {
    return {name: a.name, count: a.count + b.count}
  }
  return a;
});

console.log(result);
Raphael Rafatpanah
  • 19,082
  • 25
  • 92
  • 158
0
let all = array1.concat(array2)
//avoid dupplicates by  key
let map = all.reduce((acc,item)=>{acc[item.name] = item; return acc;},{});
//convert map back to array
let array3 = Object.values(map)
console.log(array3);
//
[ { name: 'one', count: 0 },
  { name: 'two', count: 4 },
  { name: 'three', count: 0 },
  { name: 'four', count: 2 } ]
grodzi
  • 5,633
  • 1
  • 15
  • 15
0

map can be used to call a function on each element of an array. The main difference with the version using find is that it will go through all elements of array2, so that if you have 2 elements with name: 'two' in the array, both of them will increment the count. With find only the fist found will add.

const array1 = [
  {name: 'one', count: 0}, 
  {name: 'two', count: 0}, 
  {name: 'three', count: 0},
  {name: 'four', count: 0} 
  ],
  array2 = [
  {name: 'two', count: 4},
  {name: 'four', count: 2}
  ];

const array3 = array1.map(obj => {
  let _count = obj.count;
  array2.forEach(obj2 => {
    if(obj2.name === obj.name){
      _count += obj2.count;
    }
  });
  return { name: obj.name, count: _count };
});
    
console.log(array3);

EDIT: modified following @HMR suggestion (thanks pointing my mistake) + used forEach on array2 because i don't use any returned array

Kaddath
  • 5,933
  • 1
  • 9
  • 23
  • 2
    map should not be used to mutate the array it's used on. Better to use forEach instead or return a new object. – HMR Jan 21 '19 at 14:09