omit nested properties with Lodash

- 1 answer

Ad

I'm trying to get rid of the properties 5MinuteRate and 15MinuteRatein the following object.

var object = { requestsPerSecond:
   { mean: 1710.2180279856818,
     count: 10511,
     'currentRate': 1941.4893498239829,
     '1MinuteRate': 168.08263156623656,
     '5MinuteRate': 34.74630977619571,
     '15MinuteRate': 11.646507524106095 } };

Lodash's omit()-function doesn't seem to work on nested objects. The following code doesn't work:

console.log(_.omit(object, 'requestsPerSecond.count'));

EDIT:

I tried this but it doesn't work quite right:

var subObject = _.omit(object.requestsPerSecond, '5MinuteRate', '15MinuteRate');

console.log(_.merge(object, subObject));
Ad

Answer

Ad

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>

Ad
source: stackoverflow.com
Ad