将两个单词数组与搜索栏的部分字符串进行比较
Compare two arrays of word with partial string for search bar
我是新手,我尝试学习设置一个搜索功能来查找有成分的食谱,但我被部分字符串卡住了...
即使我键入部分字符串(例如“apple”表示“apples”或“choc”表示“chocolate”),我也想查找食谱,但我只想 return 包含完整内容的食谱匹配输入的成分列表(如果有人输入“苹果汁”,他一定找不到“苹果派”)
如果输入的单词不完整,如何找到食谱?
如果有人能帮助我...
谢谢
我试着用一个简单的代码来解释我到目前为止得到的结果:
const applePie = ["apples", "pie"]
const getRecipe = function (input, recipe){
recipe.forEach((ingredient) => {
input.every((el) => recipe.includes(el)) ? console.log(recipe) : console.log("nothing found");
})
}
const test1 = ["apple"]
const test2 = ["apples"]
const test3 = ["apples", "juice"]
getRecipe(test1, applePie);
getRecipe(test2, applePie);
getRecipe(test3, applePie);
您可能想要改进搜索字典的数据结构,这将大大简化您的代码,无论您使用何种语言。所以,比如说,如果你有这个数据结构:
const cookbook = [
{
recipe : "apple pie",
ingredients: ["apple", "pie"]
},
{
recipe : "apple juice",
ingredients: ["apple", "juice"]
},
{
recipe : "milk shake",
ingredients: ["milk", "shake"]
},
{
recipe : "chocolate",
ingredients: ["cocoa", "sugar"]
}
]
那么您的搜索将大大简化为:
// will return the recipe that has "apple" in it's recipe key search
const relevantRecipe = cookbook.filter((cooks) => cooks.recipe.includes("apple"))
console.log(relevantRecipe)
这需要所有搜索键,并验证每个键都可以分配给配方的一种成分。如果在食谱的任何成分中都找不到搜索键的匹配项,代码将为给定的食谱 return false。使用您的各种食谱调用该方法,您将获得所有匹配的食谱。
const recipeMatchesIngredients = function (input, recipe){
return input.every((el) => (recipe.find((ingredient) => ingredient.startsWith(el))));
}
const applePie = ["apples", "flower"];
console.log(recipeMatchesIngredients(["app", "flower"], applePie)); // true
console.log(recipeMatchesIngredients(["app", "powder"], applePie)); // false
console.log(recipeMatchesIngredients(["apples", "juice"], applePie)); // false
console.log(recipeMatchesIngredients(["app", "flower", "pow"], applePie)); // false
我是新手,我尝试学习设置一个搜索功能来查找有成分的食谱,但我被部分字符串卡住了...
即使我键入部分字符串(例如“apple”表示“apples”或“choc”表示“chocolate”),我也想查找食谱,但我只想 return 包含完整内容的食谱匹配输入的成分列表(如果有人输入“苹果汁”,他一定找不到“苹果派”)
如果输入的单词不完整,如何找到食谱?
如果有人能帮助我...
谢谢
我试着用一个简单的代码来解释我到目前为止得到的结果:
const applePie = ["apples", "pie"]
const getRecipe = function (input, recipe){
recipe.forEach((ingredient) => {
input.every((el) => recipe.includes(el)) ? console.log(recipe) : console.log("nothing found");
})
}
const test1 = ["apple"]
const test2 = ["apples"]
const test3 = ["apples", "juice"]
getRecipe(test1, applePie);
getRecipe(test2, applePie);
getRecipe(test3, applePie);
您可能想要改进搜索字典的数据结构,这将大大简化您的代码,无论您使用何种语言。所以,比如说,如果你有这个数据结构:
const cookbook = [
{
recipe : "apple pie",
ingredients: ["apple", "pie"]
},
{
recipe : "apple juice",
ingredients: ["apple", "juice"]
},
{
recipe : "milk shake",
ingredients: ["milk", "shake"]
},
{
recipe : "chocolate",
ingredients: ["cocoa", "sugar"]
}
]
那么您的搜索将大大简化为:
// will return the recipe that has "apple" in it's recipe key search
const relevantRecipe = cookbook.filter((cooks) => cooks.recipe.includes("apple"))
console.log(relevantRecipe)
这需要所有搜索键,并验证每个键都可以分配给配方的一种成分。如果在食谱的任何成分中都找不到搜索键的匹配项,代码将为给定的食谱 return false。使用您的各种食谱调用该方法,您将获得所有匹配的食谱。
const recipeMatchesIngredients = function (input, recipe){
return input.every((el) => (recipe.find((ingredient) => ingredient.startsWith(el))));
}
const applePie = ["apples", "flower"];
console.log(recipeMatchesIngredients(["app", "flower"], applePie)); // true
console.log(recipeMatchesIngredients(["app", "powder"], applePie)); // false
console.log(recipeMatchesIngredients(["apples", "juice"], applePie)); // false
console.log(recipeMatchesIngredients(["app", "flower", "pow"], applePie)); // false