我有一个存储用户输入的数组,我有一些对象需要特定的值为真。用户输入必须与每个对象中的成分相匹配。
var recipes = [
{
'name': 'Omelette',
'ingredients': [
"Eggs",
"Mushrooms",
"Peppers",
"Onions"
]
},
{
'name': 'Spaghetti',
'ingredients': [
"Pasta",
"Tomato",
"Meat Balls"
]
};
var storedIngredients = [];
//This is the array that stores user input.
//I used this is so far but it's hardcoded and I don't want hardcode
if (storedIngredients.includes('Pasta','Tomato','Meat Balls') {
console.log(recipes.name[0];);
};
我需要一种方法,如果用户输入相应的配料,他将被显示他有制作意大利面条的配料。
最佳答案
您可以在数组上使用过滤器来查找用户具有其成分的食谱列表。
var recipes = [
{
'name': 'Omelette',
'ingredients': [
"Eggs",
"Mushrooms",
"Peppers",
"Onions"
]
},
{
'name': 'Spaghetti',
'ingredients': [
"Pasta",
"Tomato",
"Meat Balls"
]
}];
var storedIngredients = ["Pasta",
"Tomato",
"Meat Balls",
"Onions"];
var match = recipes.filter(x => x.ingredients.every(e => storedIngredients.some(s => s === e)));
console.log(match);
关于javascript - 如何检查用户输入数组是否等于特定对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52366690/