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!
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
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; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With