Skip to content

Instantly share code, notes, and snippets.

@dsottimano
Last active December 29, 2022 23:05
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 dsottimano/b4a9870d0003c5bec5ab9fc30acd5867 to your computer and use it in GitHub Desktop.
Save dsottimano/b4a9870d0003c5bec5ab9fc30acd5867 to your computer and use it in GitHub Desktop.
/**
* Recursively deletes specified keys from an object and its nested objects.
* @param {object} obj - The object to delete keys from.
* @param {string[]} keysToDelete - An array of keys to delete.
*/
function deleteKeysRecursive(obj, keysToDelete) {
for (let key in obj) {
if (keysToDelete.includes(key)) {
delete obj[key];
} else if (typeof obj[key] === 'object') {
deleteKeysRecursive(obj[key], keysToDelete);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment