Last active
April 22, 2021 15:32
-
-
Save usmansbk/3d44c7228fa8cfe8097daa2f7e2b476c to your computer and use it in GitHub Desktop.
Recursively remove json keys in an array
This file contains 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 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