omit nested properties with Lodash
Lodash _.omit works with nested objects. It appears that they improved the function since your question :)
object = _.omit(object, 'requestsPerSecond.5MinuteRate', 'requestsPerSecond.15MinuteRate');
UPDATE
omit will be removed from Lodash 5 onward
In case you need to omit some kind of paths deeply, here is an omitDeep from Deepdash:
obj = _.omitDeep(obj, /\.*5MinuteRate"\]$/);
note that this method will exclude all the matching paths at any depths. It supports a single path or array, represented as a constant value(s) or as a regex(es).
In the case of a string path given as an argument, each path in the object will be checked to end with given criteria. here is a more detailed codepen for your case
(Answer updated to fit most recent Deepdash v3.1.0)
You were almost there. Just assign what would be the result of your subObject
to object.requestsPerSecond
.
var object = {
requestsPerSecond: {
mean: 1710.2180279856818,
count: 10511,
'currentRate': 1941.4893498239829,
'1MinuteRate': 168.08263156623656,
'5MinuteRate': 34.74630977619571,
'15MinuteRate': 11.646507524106095
}
};
object.requestsPerSecond = _.omit(object.requestsPerSecond, '5MinuteRate', '15MinuteRate');
console.log(object);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
Use unset: https://lodash.com/docs#unset , it gets cleaner:
var obj = {
requestsPerSecond: {
mean: 1710.2180279856818,
count: 10511,
'currentRate': 1941.4893498239829,
'1MinuteRate': 168.08263156623656,
'5MinuteRate': 34.74630977619571,
'15MinuteRate': 11.646507524106095
}
};
_.forEach(['requestsPerSecond.5MinuteRate', 'requestsPerSecond.15MinuteRate'],
function(omitProperty) {
obj = _.unset(obj, omitProperty);
}
);
// Or avoiding the "extra" loop.
obj = _.unset(obj, 'requestsPerSecond.5MinuteRate');
obj = _.unset(obj, 'requestsPerSecond.15MinuteRate');