Friday, May 17, 2024
21
rated 0 times [  25] [ 4]  / answers: 1 / hits: 31551  / 12 Years ago, tue, january 8, 2013, 12:00:00

I have this



var matches = bookmarks.filter(function(x) {
return _.contains(x.get(tags), 'apple');
});


Which will return the bookmark objects that have the apple tags



I want to put an array there instead to pull and all the bookmarks that have the matching values, similar to this



var matches = bookmarks.filter(function(x) {
return _.contains(x.get(tags), ['apple','orange']);
});


This doesn't work, any way to get it to work?



EDIT: Im sorry, bookmarks is a collection and im trying to return the models that have the apple and orange tags


More From » underscore.js

 Answers
13

If tags is a string, your code it would be



return _.indexOf(x.get(tags), ['apple','orange']) > -1;


Example with indexOf : jsFiddle



If tags is an array, you can use intersection



return _.intersection(['apple','orange'], x.get(tags)).length > 0;


Example with intersection: jsFiddle


[#81003] Monday, January 7, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
collinisaaka

Total Points: 194
Total Questions: 105
Total Answers: 104

Location: Tonga
Member since Tue, Nov 30, 2021
3 Years ago
;