Get all models in backbone collection where attribute is NOT equal to some value
I don't know of something that's a direct opposite, but you can use a filter to the same effect.
var notMusketeers = friends.filter(function (friend) {
return friend.job !== 'Musketeer';
});
If you're using filter
directly on a Backbone collection, you must use it this way:
var notMusketeers = friends.filter(function(model){
return model.get('job') !== 'Musketeer';
});
Then notMusketeers
will be an array of Backbone model instances.
If friends
is just an array of objects (standard collection), you could use the underscore filter
this way:
var notMusketeers = _.filter(friends, function(obj){
return obj.job !== 'Musketeer';
});
ES6
If ES6+ features are available to you, const
, destructuring and arrow functions could make it a little less verbose:
const notMusketeers = friends.filter(({ job }) => job !== 'Musketeer');