Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lodash: Filter array of objects and check if not null

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.

like image 587
kenshin9 Avatar asked Apr 25 '16 18:04

kenshin9


People also ask

IS NOT NULL Lodash?

Lodash helps in working with arrays, strings, objects, numbers, etc. The _. isNull() method is used to find whether the value of the object is null. If the value is null then returns true otherwise it returns false.

How do you check if a Lodash is undefined or null?

The _. isNil() method is used to check if the value is null or undefined. If the value is nullish then returns true otherwise it returns false.

What does Lodash filter return?

Lodash helps in working with arrays, collection, strings, objects, numbers etc. The _. filter() method iterates over elements of collection, returning an array of all elements predicate returns true.

How do you filter an array in Lodash?

The filter () function has a couple convenient shorthands for dealing with arrays of objects. If you pass a string predicate instead of a function, Lodash will filter by whether that property is truthy or falsy. If your predicate is an object obj, Lodash will filter objects that match the given predicate .

How does Lodash deal with falsy values in arrays?

If the predicate returns a falsy value (like null, undefined, 0, or '' ), Lodash filters that value out. The filter () function has a couple convenient shorthands for dealing with arrays of objects. If you pass a string predicate instead of a function, Lodash will filter by whether that property is truthy or falsy.

How to check if a value is empty in Lodash?

The Lodash _.isEmpty() Method Checks if the value is an empty object, collection, map, or set. Objects are considered empty if they have no own enumerable string keyed properties. Collections are considered empty if they have a 0 length. Similarly, maps and sets are considered empty if they have a 0 size. Syntax: _.isEmpty( value )

How does Lodash filter by predicate?

If you pass a string predicate instead of a function, Lodash will filter by whether that property is truthy or falsy. If your predicate is an object obj, Lodash will filter objects that match the given predicate .


1 Answers

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

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

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

DEMO

like image 154
GG. Avatar answered Sep 23 '22 17:09

GG.