I went through SO to find the sulution but I failed...
I have two arrays of objects and I want to create the third one based on them and add some new keys/values.
Base Data
const oldArray = [
{id: 1, name: 'Test', age: 25, skill: 120},
{id: 2, name: 'Test25', age: 10, skill: 120}];
Data with some modifications
const newArray = [
{id: 1, name: 'Test2', age: 50, skill: 200},
{id: 2, name: 'Test25', age: 25, skill: 120}];
I would like to receive something like:
const expectedResultArray = [
{id: 1, oldName: 'Test', newName: 'Test2', oldAge: 25, newAge: 50, ageDifference: 25, oldSkill: 120, newSkill: 200, skillDifference: 80 },
{id: 2, oldName: 'Test25', newName:'Test25', oldAge: 10, newAge: 25, ageDifference: 15, oldSkill:120, newSkill: 120, skillDefference: 0}]
As You can see, the expected array is a combination of old and totally new calculations based on the two arrays. How can i achieve that ? I tried with reduce() however i got stuck trying:
var createNewArray = (...objects) =>
Object.values(objects.reduce((a, e) => {
a[e.id] = a[e.id] || {id: e.id};
// struggling to proceed here
return a;
}, {}))
;
console.log(createNewArr(...oldArray, ...newArray));
a[e.id] = a[e.id] || {id: e.id}? And how are you pairing items between the arrays?