Skip to content

Instantly share code, notes, and snippets.

@Jagathishrex
Created November 26, 2019 00:35
Show Gist options
  • Save Jagathishrex/0884e892f4321ffa056620cadc83dfdf to your computer and use it in GitHub Desktop.
Save Jagathishrex/0884e892f4321ffa056620cadc83dfdf to your computer and use it in GitHub Desktop.
function isObject(obj) {
var type = typeof obj;
return (type === 'function' || type === 'object') && !!obj; // this will handle obj , null and undefined
};
function deepClone(src) {
let target = {};
let keys = Object.keys(src);
for (let i = 0, len = keys.length; i < len; i++ ) {
let key = keys[i];
if (src.hasOwnProperty(key)) {
// if the value is a referece(object), recursively copy all properties by calling deepClone
let val = src[key];
let isObj = isObject(val);
if (isObj) {
target[key] = deepClone(val);
} else {
target[key] = val;
}
}
}
return target;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment