Created
October 1, 2017 15:28
-
-
Save Artem-Schander/45b3a2227d747f011e5704652dcbcb76 to your computer and use it in GitHub Desktop.
recursively remove object values by key - lodash mixin
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
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