I have two different objects that look like this:
var obj1 = {
fparams: {
keys: ['a', 'b'],
pairs: {
'p': 'qwert'
}
},
qparams: {
'x': 'xyz'
}
}
And the second object is:
var obj2 = {
fparams: {
keys: ['c', 'd'],
pairs: {
'q': 'yuiop'
}
},
qparams: {
'z': 'zyx'
}
}
How can I merge the properties from obj2
into obj1
?
While working in Angular, I attempted using angular.merge(obj1,obj2)
, however it didn't merge the 'keys' array, instead replaced it with the values from obj2
. Other properties were merged successfully.
This is the desired outcome:
var obj2 = {
fparams: {
keys: ['a', 'b', 'c', 'd'],
pairs: {
'p': 'qwert',
'q': 'yuiop'
}
},
qparams: {
'x': 'xyz',
'y': 'zyx'
}
}
The version of Angular I'm using is angular 1.5.8.
Edit : I eventually switched to lodash because it was more convenient to work with and offered many functionalities that I wasn't aware of previously.