Created
August 6, 2016 10:21
-
-
Save dbilovd/cc7f745e47923b77ebd0ab8c1537788b to your computer and use it in GitHub Desktop.
Update object properties with values from another object.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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