2

I have two arrays of objects and need to move the value from the 2nd array in to the first array object with the same id.

array1 = [{id:1, location: 'A'},{id:2, location: 'B'},{id:3, location: 'C'},{id:4, location: 'D'}]

array2 = [{id:1, value: 123},{id:2, value: 5466},{id:3, value: 89484},{id:4, value: -4.215}]

Ive tried merging them but i just end up with duplicate objects.

how i want it to end up looking like

array1 = [{id:1, location: 'A', value: 123},{id:2, location: 'B', value: 5466},{id:3, location: 'C', value: 89484},{id:4, location: 'D', value: -4.215}]

4 Answers 4

1

You may traverse your base array with Array.prototype.map(), using Array.prototype.find() along the way to lookup another array for matching id:

const array1 = [{id:1, location: 'A'},{id:2, location: 'B'},{id:3, location: 'C'},{id:4, location: 'D'}],
      array2 = [{id:1, value: 123},{id:2, value: 5466},{id:3, value: 89484},{id:4, value: -4.215}],
      
      result = array1.map(o => ({...o, ...array2.find(_o => _o.id == o.id)}))
      
console.log(result)
.as-console-wrapper{min-height:100%;}

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

Comments

0

Please find below snippet

array1 = [{id:1, location: 'A'},{id:2, location: 'B'},{id:3, location: 'C'},{id:4, location: 'D'}]

array2 = [{id:1, value: 123},{id:2, value: 5466},{id:3, value: 89484},{id:4, value: -4.215}]

let result = array1.map(obj => {
  return {
        ...array2.filter(obj2 => obj2.id === obj.id)[0],
        ...obj
   }
})

console.log(result)

1 Comment

This will loop unnecessarily throughout the entire array2 even when the match (unique) was already found. So, .find()-approach is more optimal from performance standpoint.
0

This should work:

array1.map( // build a second array with the new objects
       el => Object.assign( // create the merged object
               el, //          from the current object
               array2.find( // with the object in array2 with the same id
                    obj => obj.id == el.id
               )
       )
)

1 Comment

This would also mutate array1 behind the scenes, so using .map() rather than .forEach() doesn't really make much sense. If you need to stay immutable, you would need to do Object.assign({}, el, array2.find..
0

If index are same you can map and spread the object like below

const array1 = [{id:1, location: 'A'},{id:2, location: 'B'},{id:3, location: 'C'},{id:4, location: 'D'}];

const array2 = [{id:1, value: 123},{id:2, value: 5466},{id:3, value: 89484},{id:4, value: -4.215}];

const merged = array1.map((a,i)=>({...a, ...array2[i]}));
 
console.log(merged); 

If the index are not same then you can do like below

const array1 = [{id:1, location: 'A'},{id:2, location: 'B'},{id:3, location: 'C'},{id:4, location: 'D'}];

const array2 = [{id:1, value: 123},{id:2, value: 5466},{id:3, value: 89484},{id:4, value: -4.215}];
  
const merged = array1.map(a=>({...a, ...array2.find(a2=>a2.id==a.id)}));

console.log(merged);

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.