I want to have a immutable original array of object and extend a new one, but seems the code below did nto produce the right result. obj3 became an object instead of an array of object. I even tried lodash's extend.
var obj1 = [{
abc: 'abc value'
}, {
def: 'def value'
}];
var obj2 = {
def: 'new def value'
};
/*var obj3 = _.extend({}, obj1, obj2);
console.log(obj3)
*/
var obj3 = Object.assign({},obj1,obj2);
https://jsbin.com/bawadeqicu/edit?html,js,output
My desire output is
obj1: [{"abc":"abc value"},{"def":"def value"}] // immutable
obj3: [{"abc":"abc value"},{"def":"new def value"}]
obj2as the second element of the new copy. There is no "pretty" way to do this.var obj3 = obj1.slice(); obj3[1] = obj2;. I mean, you could dovar obj3 = Object.assign([], obj1, {1: obj2});but that seems more magic ¯\_(ツ)_/¯def? Do you want to match/merge objects based on property? What should happen if the property name was different? wouldobj2simply be added to the array? Would the objects be merged in a different? What if both objects in the array had the same property? What if they have other properties as well? You haven't actually described what the merge rules are so any answer you get will just be an interpretation based on the specific input and output you provided.