Cde updating please wait

", Array(a[1])); a = []; a.merge(bo); assert True("Object stays Object? Array(a[1])); a = []; a.merge(b); a.merge(bo); assert True("Object overrides Array", !

cde updating please wait-72

Also, in your example, it might be worth adding something into o2 that isn't originally in o1, to show that o2 properties get inserted into o1 (someone else below submitted alternative code copying your example, but theirs breaks when o2 contains properties not in o1 - but yours works in this respect).

This is a good answer, but a couple quibbles: 1) No logic should be based on exceptions; in this case, any exception thrown will be caught with unpredictable results.

I don't mean to harp on you but saying that it's okie because does is a poor argument.

is popular but it doesn't mean that they are the role model on how to do Java Script.

Joachim Lou's comment has received well deserved upvotes and provides that extra information when needed.

I think the difference between deep and shallow merging and similar topics are beyond the scope of this discussion and are better suited to other SO questions.

The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. The best way for you to do this is to add a proper property that is non-enumerable using Object.define Property.

This way you will still be able to iterate over your objects properties without having the newly created "extend" that you would get if you were to create the property with Object.prototype.extend.

Side note: if you don't want to add obj2 to obj1, you can return a new object using @Kermani The OP specifically points out that recursion is unnecessary.

Therefore, while it is useful to know that this solution performs a shallow merge, this answer is sufficient as is.

Like the j Query answer, to do this mutation-free just merge into an empty object: I need to merge objects today, and this question (and answers) helped me a lot.

Tags: , ,