Skip to content

Instantly share code, notes, and snippets.

@Artem-Schander
Created October 1, 2017 15:28
Show Gist options
  • Save Artem-Schander/45b3a2227d747f011e5704652dcbcb76 to your computer and use it in GitHub Desktop.
Save Artem-Schander/45b3a2227d747f011e5704652dcbcb76 to your computer and use it in GitHub Desktop.
recursively remove object values by key - lodash mixin
import _ from 'lodash';
// usage: _.removeDeepByKey(someObject, 'unwantedKey');
_.mixin({
'removeDeepByKey': function(obj, keyToBeRemoved) {
return _.transform(obj, function(result, value, key) {
if (_.isObject(value)) {
value = _.removeDeepByKey(value, keyToBeRemoved);
}
if (key !== keyToBeRemoved) {
_.isArray(obj) ? result.push(value) : result[key] = value;
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment