Search code examples
javascriptarraysjavascript-objects

Merge each JavaScript object in huge array with another object


I have an array of JavaScript object which has over millions of objects, I want to merge each object in the array with a specific object with fixed key and values

[{prop1:"a",prop2:"b"}, {prop1:"c",prop2:"d"}] -- Array of object


{id:"1","dept":"Finance"} -- object to be merge in

Currently I am iterating over each object in array and adding the key and value one by one which is very time consuming.

I am looking for an alternate solution in JavaScript to merge two object like a bulk merge without iterating on each object to save CPU time.


Solution

  • Use Object.assign() to merge objects.

    array_of_objects.forEach(obj => Object.assign(obj, object_to_merge_in));