Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
10
rated 0 times [  17] [ 7]  / answers: 1 / hits: 47480  / 8 Years ago, mon, april 25, 2016, 12:00:00

I just started using lodash and have this array of objects, where one of the properties either has an integer or is null. I know how to filter the array for items that are null, but how do I check if it's not null?



Let's say I have something like this:



var users = [
{ 'user': 'barney', 'default': 1 },
{ 'user': 'dino', 'default': 0 },
{ 'user': 'wilma', 'default': 1 },
{ 'user': 'fred', 'default': null }
];


And then I want something like:



var notNullDefault = _.filter(sourceData, ['is_default', ! null ]); // objects with names barney, dino, wilma
var nullDefault = _.filter(sourceData, ['is_default', null ]); // object with name fred


Again, I'm new to lodash, so maybe there's a better way to accomplish this too.



Thanks in advance.


More From » arrays

 Answers
10

This can be solved with _.reject and _.filter:



var notNullDefault = _.reject(sourceData, ['default', null]);

var nullDefault = _.filter(sourceData, ['default', null]);


DEMO


[#62404] Saturday, April 23, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mckaylab

Total Points: 311
Total Questions: 120
Total Answers: 93

Location: Montenegro
Member since Thu, Jun 16, 2022
2 Years ago
;