0

我想检查是否可以使用输入的成分制作食谱。我尝试换出数组,但它不正确匹配。有没有其他方法可以检查食谱是否可以制作?

recipes = [
    {
        "category": "Dezerti",
        "ingredients": ["10 belanaca", "500 g secera", "2 kasike sirceta", "2 kasike gustina", "10 zumanaca", "10 kasika secera", "5 kasika gustina", "2 kesice vanile", "8 dl mleka", "250 g putera", "200 g mlevenih oraha", "250 ml slatke pavlake", "250 ml slatke pavlake"],
        "link": "https://www.recepti.com/kuvar/torte/52213-torta-spanski-vetar",
        "name": "Torta španski vetar (3)"
    },
    {
        "category": "Dezerti",
        "ingredients": ["200 g mlevena plazma"],
        "link": "Test",
        "name": "Test"
    },
];

这是我尝试过的:

const options = {
    includeScore: true,
    keys: ['ingredients'],
    includeMatches: true,
    threshold: 0.4,
}


class Ingredient {
    constructor(ingredients) {
        this.ingredients = ingredients;
    }
}

function canBeMade(recipe, ingredients) {

    var p = recipe.ingredients;
    recipe.ingredients = ingredients;
    ingredients = p;

    ingObj = [];

    res = [];
    res.push(recipe);

    fuse = new Fuse(res, options);

    for (let i = 0; i < ingredients.length; i++) {

        ingObj.push(new Ingredient(ingredients[i]));

    }

    result = fuse.search({
        $and: ingObj
    });

    if (result[0] != undefined)
        return true;

    return false;

}

返回假

console.log(canBeMade(recipes[1], ["plazma"]));
4

0 回答 0