I want to make a function that receives two arrays and returns a new array. The function needs to:
- compare two arrays of objects by a key('name')
- For elements with duplicate keys, add the values('grade').
- For elements with unique keys (no duplicates), just push the element as it is in the resulting array.
For example, with these two arrays arr1 and arr2
const arr1 = [{"name": "A", "grade": 50},{"name": "B","grade": 30},{"name": "C", "grade": 80}];
const arr2 = [{"name": "A", "grade": 70},{"name": "B","grade": 10}];
I want to return a new array such as:
const res = [{"name": "A", "grade": 120},{"name": "B","grade": 40},{"name": "C", "grade": 80}];
I tried nested loops with forEach and map but nothing seemed to work. I would very much appreciate your help. Thank you in advance!