Skip to content

Instantly share code, notes, and snippets.

@Sanix-Darker
Created February 21, 2018 07:32
Show Gist options
  • Save Sanix-Darker/8f29f7b6276068c0480fbe49259c5201 to your computer and use it in GitHub Desktop.
Save Sanix-Darker/8f29f7b6276068c0480fbe49259c5201 to your computer and use it in GitHub Desktop.
[JS] angular merge angular.merge FOR OLDER VERSIONS
function merge(obj1,obj2){ // Our merge function
var result = {}; // return result
for(var i in obj1){ // for every property in obj1
if((i in obj2) && (typeof obj1[i] === "object") && (i !== null)){
result[i] = merge(obj1[i],obj2[i]); // if it's an object, merge
}else{
result[i] = obj1[i]; // add it to result
}
}
for(i in obj2){ // add the remaining properties from object 2
if(i in result){ //conflict
continue;
}
result[i] = obj2[i];
}
return result;
}
// ORRRRRRRRRRRRRRRRRRRRR
function merge(dst){
var slice = [].slice;
var isArray = Array.isArray;
function baseExtend(dst, objs, deep) {
for (var i = 0, ii = objs.length; i < ii; ++i) {
var obj = objs[i];
if (!angular.isObject(obj) && !angular.isFunction(obj)) continue;
var keys = Object.keys(obj);
for (var j = 0, jj = keys.length; j < jj; j++) {
var key = keys[j];
var src = obj[key];
if (deep && angular.isObject(src)) {
if (!angular.isObject(dst[key])) dst[key] = isArray(src) ? [] : {};
baseExtend(dst[key], [src], true);
} else {
dst[key] = src;
}
}
}
return dst;
}
return baseExtend(dst, slice.call(arguments, 1), true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment