0

Is there a way to simplify this code with the use of Object.entries()? I want to remove new Map().

const err = [{ 
 'id': 1, 
 'error': ["Error 1", "Error2"]
}]

const warn = [{ 
 'id': 1, 
 'warning': ["Warn 1", "Warn 2"]
}]

const map = new Map();
err.forEach(item=> map.set(item.id, item));
warn.forEach(item=> map.set(item.id, {...map.get(item.id), ...item}));
const combined = Array.from(map.values());
console.log(combined)

Tried:

const map = new Map(Object.entries(err));
warn.forEach(item=> map.set(item.id, {...map.get(item.id), ...item}));
const combined = Array.from(map.values());
console.log(combined)

The output should still be the same

[{ 
 'id': 1, 
 'error': ["Error 1", "Error2"],
 'warning': ["Warn 1", "Warn 2"] 
}]
Oashi
  • 39
  • 7
  • 1
    Object entries is for object, not an array – IT goldman Jul 21 '22 at 12:38
  • Does this answer your question? [JavaScript merging objects by id](https://stackoverflow.com/questions/19480008/javascript-merging-objects-by-id) – pilchard Jul 21 '22 at 12:57
  • also: [Merge two array of objects based on a key](https://stackoverflow.com/questions/46849286/merge-two-array-of-objects-based-on-a-key) (but it looks like you already copied your code from [this one](https://stackoverflow.com/a/60365470/13762301)) – pilchard Jul 21 '22 at 12:58

3 Answers3

1

You can use Array.prototype.map() to create the key/value pairs for the new Map() argument.

const err = [{
  'id': 1,
  'error': ["Error 1", "Error 2"]
}]

const warn = [{
  'id': 1,
  'warning': ["Warn 1", "Warn 2"]
}]


const map = new Map(err.map(el => [el.id, el]));
warn.forEach(el => map.get(el.id).warning = el.warning);
const combined = Array.from(map.values());
console.log(combined)
Object.entries() isn't useful because the keys are the array indexes, not the id properties.
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

If you expect more than 1 item per array you can do:

const err = [{ 
 'id': 1, 
 'error': ["Error 1", "Error2"]
}]

const warn = [{ 
 'id': 1, 
 'warning': ["Warn 1", "Warn 2"]
}]


const newObj = err.map( (item,i) => Object.assign({},item,warn[i]));

console.log(newObj)

If the final object depends on those other arrays and you know in advance that they have exactly length 1 then is simpler:

const err = [{ 
 'id': 1, 
 'error': ["Error 1", "Error2"]
}]

const warn = [{ 
 'id': 1, 
 'warning': ["Warn 1", "Warn 2"]
}]


const newArr = [Object.assign({},err[0],warn[0])]

console.log(newArr)
malarres
  • 2,941
  • 1
  • 21
  • 35
  • I think this wont work if i add id:2 with error in err. ** { 'id': 2, 'error': ['err3','err4']} ** – Oashi Jul 21 '22 at 12:55
  • Based on your example i was assuming that you wanted to merge elements with same id assured. But if you have a more complex scenario in mind please share it. Maybe rather than iterating on both at once you need to find them – malarres Jul 21 '22 at 14:10
0

Object entries is for object, not an array. However you can group by using reduce into object, then get his values to turn into array.

const err = [{
  'id': 1,
  'error': ["Error 1", "Error 2"]
}]

const warn = [{
  'id': 1,
  'warning': ["Warn 1", "Warn 2"]
}]


var combined = Object.values(err.concat(warn).reduce(function(agg, item) {
  agg[item.id] = { ...agg[item.id], ...item}
  return agg;
}, {}));
console.log(combined)
IT goldman
  • 14,885
  • 2
  • 14
  • 28