Skip to content

Instantly share code, notes, and snippets.

@nadavrt
Last active October 24, 2018 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nadavrt/8d429e8207392f704fb2c049f12e4ace to your computer and use it in GitHub Desktop.
Save nadavrt/8d429e8207392f704fb2c049f12e4ace to your computer and use it in GitHub Desktop.
A one-line JavaScript function that merges two objects.
/**
* Merge two objects. Warning: this function permanently changes obj1 due to it being passed by reference.
* If a property/method exists in both objects obj2's will overwrite the value that existed in obj1.
* @param obj1 The object to merge into.
* @param obj2 An object with properties to add to obj1.
* @return NULL
**/
function mergeObjects(obj1, obj2)
{
Object.keys(obj2).forEach(function(key) { obj1[key] = obj2[key]; });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment