Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

omit nested properties with Lodash

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));
like image 308
Hedge Avatar asked Dec 20 '15 00:12

Hedge


4 Answers

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>
like image 154
J. Titus Avatar answered Nov 11 '22 13:11

J. Titus


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');
like image 21
aemonge Avatar answered Nov 11 '22 14:11

aemonge


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

like image 5
Claudiu Avatar answered Nov 11 '22 14:11

Claudiu


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)

like image 2
Yuri Gor Avatar answered Nov 11 '22 13:11

Yuri Gor