0

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.

4
  • 2
    How do you expect to be able to alter every object in the array without iterating over each one? You can't get more efficient than O(n) and it sounds like you've already tried that solution. Commented Dec 20, 2019 at 6:47
  • Why not add these props while creating these objects. Commented Dec 20, 2019 at 6:47
  • You could wrap the array with another object, implementing the api you need, that only merges the given objects on access. Sort of a lazy merge. Though feasibility it depends on how that outer containing array is passed around / used. Commented Dec 20, 2019 at 6:54
  • @ElanHamburger, I have no idea if that is possible, as in past I have done object key conversion to string using regular expression without iterating over each object. Commented Dec 20, 2019 at 6:59

3 Answers 3

1

Use Object.assign() to merge objects.

array_of_objects.forEach(obj => Object.assign(obj, object_to_merge_in));
Sign up to request clarification or add additional context in comments.

4 Comments

will it be faster than iterating on each object and adding the key value pairs individually ?
I'd expect it to be, since it's highly optimized code in the JavaScript internals.
@RohanKumar I think a more realistic test is needed. jsben.ch/WZ2OV
@RohanKumar On your test my version is fastest by 5%. But the main difference is the reduced memory, which is not apparent with only 2 elements in the array.
0

At first for performance-wise, it is O(n) complexity. So there is no way out without doing a loop through each object whether you use Object.assign, array.map or any other method.

If you have so many json object manipulation related works in your project, life will be easier if you use lodash.

There are plenty of methods as your requirement in that library

    _.merge(object, [sources], [customizer], [thisArg])
    _.assign(object, [sources], [customizer], [thisArg])
    _.extend(object, [sources], [customizer], [thisArg])
    _.defaults(object, [sources])
    _.defaultsDeep(object, [sources])

Comments

-1

You can use .map() without using of loop to merge a new object in array of objects

var objArr = [{prop1:"a",prop2:"b"}, {prop1:"c",prop2:"d"}],
    obj = {id:"1","dept":"Finance"} ;

var mapArr = objArr.map(x => Object.assign(x,obj));

console.log(mapArr);

8 Comments

Why create a new list with map()? Object.assign() modifies the object in place.
@Barmar Map is used to iterate an array internally, and Object.assign() is used to assign/copy an object from another. So we need to use map to iterate each object then copy a new Object to an existing one.
Object.assign() modifies the first argument in place, it doesn't create a new object.
You only get new objects if you write Object.assign({}, x, obj)
With over a million objects in the array, creating new copies might be a memory issue.
|

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.