I want to be able to merge two JavaScript objects with same properties/keys defined. Just that the values are not duplicate. For example I'd like to:
var obj1 = { 0:{id: 33, name: 'abc', date: "12/12/12", type:"11" }, 1: {id: 33, name: 'abc3', date: "12/13/12", type:"131"} } var obj2 = { 0:{id: 22, name: 'abc1', date: "12/1/13", type: "33" } 1:{id: 4, name: 'abc4', date: "12/4/12", type:"14"} } here is the fiddle: http://jsfiddle.net/q9c8k2yh/ this is the expected structure:
var obj3 = { 0: { id: 33, name: 'abc', date: "12/12/12", type:"11" }, 1: { id: 22, name: 'abc1', date: "12/1/13", type: "33" } } I looked up at the sources online and the most common answer I get:
Object.assign(obj2, obj1); I have tried:
obj3 = Object.assign(obj2, obj1);//returns { id: 33, name: 'abc', date: "12/12/12", type:"11" } obj3 = Object.assign(obj1, obj2);//return { id: 22, name: 'abc1', date: "12/1/13", type: "33" } obj3 = Object.assign({},obj2, obj1); //return { id: 33, name: 'abc', date: "12/12/12", type:"11" } but none of them gives me both the objects. I do not want to replace one over the other. is there a way to achieve this?
Thanks!

{0: obj1, 1: obj2}? If you have a small definite number of them at least.