-
-
Save loretoparisi/475876d7ae408e2ea17aaf9c5fd09286 to your computer and use it in GitHub Desktop.
Recursively remove json keys in an array
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 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