Skip to content

Instantly share code, notes, and snippets.

@msmiley
Created October 8, 2019 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msmiley/66df9f78b7f40dc5b8118372bcf71636 to your computer and use it in GitHub Desktop.
Save msmiley/66df9f78b7f40dc5b8118372bcf71636 to your computer and use it in GitHub Desktop.
js cloneDeepNoFunctions
exports.cloneDeepNoFunctions = function(obj) {
let ret;
// set up recursion for arrays and objects
if (Array.isArray(obj)) {
ret = [];
// iterate
for (let v of obj) {
if (typeof(v) === 'function') {
continue;
} else if (v instanceof Object || v instanceof Array) {
ret.push(exports.cloneDeepNoFunctions(v));
} else {
ret.push(v);
}
}
} else if (obj instanceof Object) {
ret = {};
// iterate
for (let [k,v] of Object.entries(obj)) {
if (typeof(v) === 'function') {
continue;
} else if (v instanceof Object || v instanceof Array) {
ret[k] = exports.cloneDeepNoFunctions(v);
} else {
ret[k] = v;
}
}
} else {
ret = obj;
}
return ret;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment