Skip to content

Instantly share code, notes, and snippets.

@usmansbk
Last active April 22, 2021 15:32
Show Gist options
  • Save usmansbk/3d44c7228fa8cfe8097daa2f7e2b476c to your computer and use it in GitHub Desktop.
Save usmansbk/3d44c7228fa8cfe8097daa2f7e2b476c to your computer and use it in GitHub Desktop.
Recursively remove json keys in an array
/**
* @function stripJSON
* @desc - This function removes selected object keys
* @param {Object} json - JavaScript object to strip
* @param {Object[]} keys - array of selected keys (string)
* @return {Object} - deep copy of object without keys
*/
function stripJSON(json, keys) {
if (json === null || json === undefined) return json;
let obj = {}, key;
for (key in json) {
let inside = keys.indexOf(key);
if (inside !== -1) continue;
if (typeof json[key] === 'object') {
obj[key] = stripJSON(json[key], keys);
} else {
obj[key] = json[key];
}
}
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment