For instance, from these two objects :
var object1 = {
"color": "yellow",
"size": null,
"age": 7,
"weight": null
}
var object2 = {
"color": "blue",
"size": 51,
"age": null
}
I want this (object2
overrides object1
except for null
properties or properties he doesn't have):
{
"color": "blue",
"size": 51,
"age": 7,
"weight": null
}
Copy
Extend:
Merge:
If you want to not overwrite with null, you can use this.
Object.assign():
Sources:
https://docs.angularjs.org/api/ng/function
http://davidcai.github.io/blog/posts/copy-vs-extend-vs-merge/
Using angualr.extend will not produce the result requested. The object2.age null value will override object1.age value.
angular.extend(object1, object2) will produce the following result:
Use the following code to skip over null properties
This will produce the following requested result
For newer versions (at least 1.4.0) of angular you can use
angular.merge