RyanP13
RyanP13

Reputation: 7743

jQuery grep method for more than one type of filter

I am currently using the jQuery $.grep() method to filter an array by a value passed to the method like so:

var filteredArray = $.grep(arr, function (n, i) {
    return (n.type === type);
}, true);

So the array is an array of objects that have a type property and if that property is equal to the type passed to it then filter out the other objects and produce a new array from the result.

How do i extend this to accept multiple types of filters?

So if i pass an array of filters that can be a minimum length of 1 and nay maximum.

Upvotes: 1

Views: 2025

Answers (1)

gdoron
gdoron

Reputation: 150253

var types = [1, 2]; // example of types...

var filteredArray = $.grep(arr, function(element) {
    return $.inArray(element.type, types) >= 0;
});

console.log(filteredArray);​

Live DEMO

Upvotes: 1

Related Questions