Skip to content

Instantly share code, notes, and snippets.

@dbilovd
Created August 6, 2016 10:21
Show Gist options
  • Save dbilovd/cc7f745e47923b77ebd0ab8c1537788b to your computer and use it in GitHub Desktop.
Save dbilovd/cc7f745e47923b77ebd0ab8c1537788b to your computer and use it in GitHub Desktop.
Update object properties with values from another object.
var objA = {
"a" : 1,
"b" : 2
},
objB = {
"a" : 3,
"c" : 4
};
var updateObj = function (a, b) {
var aKeys = Object.keys(a);
aKeys.forEach(function (aKey) {
if (typeof b[aKey] !== "undefined" && b[aKey] !== "") {
a[aKey] = b[aKey];
}
});
return a;
}
var updatedObj = updateObj(objA, objB);
console.log(updatedObj);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment