Created
November 26, 2019 00:35
-
-
Save Jagathishrex/0884e892f4321ffa056620cadc83dfdf to your computer and use it in GitHub Desktop.
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
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