Created
March 31, 2015 07:34
-
-
Save philippspinnler/63c916af1e2594179561 to your computer and use it in GitHub Desktop.
Angular function to merge two objects. This method will be in angular 1.4 but is missing in previous versions.
This file contains 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
function merge(dst, src) { | |
var h = dst.$$hashKey; | |
if (!angular.isObject(src) && !angular.isFunction(src)) return; | |
var keys = Object.keys(src); | |
for (var j = 0, jj = keys.length; j < jj; j++) { | |
var key = keys[j]; | |
var src_new = src[key]; | |
if (angular.isObject(src_new)) { | |
if (!angular.isObject(dst[key])) dst[key] = angular.isArray(src_new) ? [] : {}; | |
merge(dst[key], src_new); | |
} else { | |
dst[key] = src_new; | |
} | |
} | |
if (h) { | |
dst.$$hashKey = h; | |
} else { | |
delete dst.$$hashKey; | |
} | |
return dst; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment