Javascript filter check for multiple values?
Change your filter to :
var countriesFound = countries.filter(function(country) {
return ["Spain","Greece"].indexOf(country.key) != -1
});
Where ["Spain","Greece"]
is your list of valid countries that you want to filter by. The value returned form the filter function applied to the array determines whether the element is filtered in or out. indexOf
finds the position in the given array of the give country.key
As per the add the filtered array to the beginning of the other one you are correct, doing countries.unshift(countriesFound)
after the previous code will achieve that.
The same answer as @Juan Corés, but with arrow function expression:
const countriesFound = countries.filter(country =>
["Spain","Greece"].indexOf(country.key) != -1
);