I have an array of object and need to check if the any value of a second array is included within a specific field.
My array is called cocktailList and the field I need to check against is called "ingredients".
The second array is called selectedOptions.
The following code works perfectly when the second array has only 1 value.
Example
const selectedOptions = ["vodka"];
const selectedDrinks = cocktailList.filter(function (item, i) {
if (item.ingredients.toLowerCase().includes(selectedOptions)) {
return true;
} else false;
});
The problem occurs when I have more than 1 value in selectedOptions My objective is to return TRUE if the item.ingredients contains ANY value of selectedOptions.
If I have the following:
const selectedOptions = ["vodka", "rum"];
It would only return true if the ingredients field contains all values of selectedOptions
I've tried the following solution as suggested by others
if (item.ingredients.toLowerCase().some((val) => selectedOptions.indexOf(val) !== -1))
but I'm getting an error
TypeError: item.ingredients.toLowerCase().some is not a function. (In 'item.ingredients.toLowerCase().some(function (i)
Each item is an object which contains "ingredients" field containing a string of this kind
Object {
"alcoholic": "true",
"complete": false,
"country": "cuba",
"dairy_free": "",
"description": "Made without the traditional orange liqueur",
"difficulty": "",
"dosage": "1½ shot(s) Tequila, ½ shot(s) Lime Juice, ½ shot(s)",
"drinkId": "1103",
"equipment": "Shaker, Strainer",
"garnish": "",
"glass": "collins",
"ingredients": "Tequila, Lime Juice, Agave Nectar Juice",
}
cocktailList. It looks like in the original code, theitem.ingredientsis a string but in the other example it is not?selectedOptions.some(…)then, notingredients.….some(…).const selectedDrinks = cocktailList.filter(function (item, i) { if (selectedOptions.some(item.ingredients.toLowerCase())) { return true; } else false; });