I have a collection with models that all have an array of tags.
If I wanted to grab a model that had one tag I would do like so:
models.filter(function(x) {
return _.contains(x.get("tags"), 'google');
});
This would grab the models that have the tag google. The problem I am having is that I cant put more that one tag there, I need for it to be an array. How would I go about grabbing all the models that have multiple tags (ex: google, yahoo, apple)? The contains method only supports 1 string and not an array
You could use
_.intersectionthusly:The
_.intersection(a, b)will give you an array with all the elements that are common to bothaandb, if the intersection is the same size as the array/set you’re looking for thenm.get('tags')has everything you’re looking for.