How can I merge properties of two JavaScript objects dynamically?

Posted by JC Grubbs on Stack Overflow See other posts from Stack Overflow or by JC Grubbs
Published on 2008-10-05T00:30:54Z Indexed on 2010/03/25 13:43 UTC
Read the original article Hit count: 329

Filed under:

I need to be able to merge two (very simple) JavaScript objects at runtime. For example I'd like to:

var obj1 = { food: 'pizza', car: 'ford' }
var obj2 = { animal: 'dog' }

obj1.merge(obj2);

//obj1 now has three properties: food, car, and animal

Does anyone have a script for this or know of a built in way to do this? I do not need recursion, and I do not need to merge functions, just methods on flat objects.

© Stack Overflow or respective owner

Related posts about JavaScript