Find multiple lines with the 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 title = "Hat" using the _.where function.

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

But what if I want to search for an entire object that contains title = "Hat" or "Shirt"? Can this be done with the same _.where function?

Thanks in advance

+4
source share
1 answer

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

 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

+7
source

Source: https://habr.com/ru/post/1498267/


All Articles