Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

opposite of _.where(list, properties)

I have an array of objects and I set Selected = true to some customers. Using _.where I'm getting a new array which have only the selected customers. Is there any method to get the customers who don't have this attribute? I don't want to set Selected = false to the rest of the customers and grab them by

_.where(customers, {Selected: false}); 

Thank you very much!

like image 220
jimakos17 Avatar asked Dec 26 '22 04:12

jimakos17


2 Answers

use _.reject

_.reject(customers, function(cust) { return cust.Selected; });

Docs: http://underscorejs.org/#reject

Returns the values in list without the elements that the truth test (iterator) passes. The opposite of filter.

Another option if you need this particular logic a lot: You could also create your own Underscore Mixin with: _.mixin and create a _.whereNot function and keep the nice short syntax of _.where

like image 101
BLSully Avatar answered Jan 07 '23 14:01

BLSully


you could do it this way, if you are sure that the property will not be there:

_.where(customers, {Selected: undefined});

this won't work if the object has Selected: false

You could also use _.filter which would probably be better:

_.filter(customers, function(o) { return !o.Selected; });
like image 28
Austin Greco Avatar answered Jan 07 '23 15:01

Austin Greco