Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find more than one string with underscore.js _.where function

lets say that I have this array of objects.

var initialData = [{Title:"Hat",Price:49.95},
                   {Title:"Pants",Price:78.25},                 
                  {Title:"Shirt",Price:12.34}];

I know that I can find which objects have the Title = "Hat" using the _.where function.

// underscore method
console.log( _.where(initialData, {Title:"Hat"}));

But what if I want to look for all the object that contain Title = "Hat" or "Shirt"? Is possible to do it with the same _.where function?

Thanks in advance

like image 351
Bob James Avatar asked Mar 23 '23 16:03

Bob James


1 Answers

Thanks Simon. I did what you suggested, and the code below is working.

var initialData = [{Title:"Hat",Price:49.95},
                   {Title:"Pants",Price:78.25},                 
                  {Title:"Shirt",Price:12.34}];

var match=['Hat', 'Shirt'];

//underscore method
console.log( _.filter(initialData, function(num){ return _.contains(match,num.Title) }));

Thanks

like image 179
Bob James Avatar answered Apr 25 '23 15:04

Bob James