【问题标题】:Matching two array with flexibility灵活匹配两个阵列
【发布时间】:2021-04-19 02:07:36
【问题描述】:

我正在开发一个 Ionic 应用程序,该应用程序包含成分数组和一个带有食谱的服务,看起来像这样->

所以我需要将配料与食谱相匹配。我开始这样做了:

 this.truelist = [...this.spliterStrIngredients()]; // my arrayof ingredients
 const  nbIngredients = this.truelist.length;

 console.log('true list = ' + this.truelist);
 let match = 0 ;
 this.recipeList.forEach((key => {
     key.ingreds.forEach(ingrRecipe => {
         this.truelist.forEach((ingrSelct , index) => {
            if (ingrSelct === ingrRecipe)
            {
                match ++;
            }
            if (match >= this.truelist.length ) {
                this.recipeMatch.push(key.name);
                match = 0;
            }
         });
     });

 }));
 console.log(this.recipeMatch);
}

所以当我选择完全相同的食谱时它可以工作,但我希望更灵活,例如我有意大利面+奶酪+胡萝卜的数组,但我可以搭配 carbonara。

感谢您的帮助

【问题讨论】:

  • 我无法弄清楚你的灵活匹配规则。如果你能说出灵活匹配的确切规则就更好了。我不太了解如何制作 Carbonara(笑)
  • 所以 Carbonara = [pasta ,cheese ] 但即使我有更多的配料,我也会匹配。例如,我的配料中有 [意大利面、奶酪、其他配料更多]。我的意思是,如果我的阵列中有好的成分和更多成分,它就是匹配的。如果很清楚,我不知道

标签: javascript angular typescript ionic-framework types


【解决方案1】:

阅读您的解释后,我想出了以下解决方案。 我对 typescript 不熟悉,所以我用纯 JavaScript 编写如下:

const carbonara = ['pasta', 'cheese'];
const menuA = ['pasta', 'cheese', 'carrot'];
const menuB = ['pasta', 'tomato'];
const menuC = ['carrot', 'pasta', 'cheese'];

const flexibleMatch = (menu1, menu2) => {
  for (let i = 0; i < menu1.length; i++) {
    if (!menu2.includes(menu1[i])) {
        return false;
    }
  }
  return true;
};

console.log(flexibleMatch(carbonara, menuA)); // true: menuA includes all ingredients in Carbonara plus carrot
console.log(flexibleMatch(carbonara, menuB)); // false: menuB doesn't contain cheese
console.log(flexibleMatch(carbonara, menuC)); // true: ingredients order doesn't matter

【讨论】:

  • @Clementwdk 如你所说,我添加了一个 menuC,你可以在 jsfiddle 或其他地方尝试一下
【解决方案2】:

按照您提出的规则,这里有一个适合您需求的解决方案:

const recipeList = [{
    name: 'Carbonara',
    picFile: '',
    ingreds: ['Pasta', 'Cheese', 'Egg'],
    description: ['1', '2', '3']
}];

const myIngredientsA = ['Pasta', 'Cheese'];
const myIngredientsB = ['Pasta', 'Cheese', 'Egg'];
const myIngredientsC = ['Pasta', 'Cheese', 'Egg', 'Bacon'];

function findMatchRecipe(myIngredientsList: string[]) {
    let count = 0;
    let recipeMatch: any[] = [];

    recipeList.forEach(recipe => {
        recipe.ingreds.forEach(ingredient => {
            if (myIngredientsList.includes(ingredient)) {
                count++;
            }
        });
    
        if (count === recipe.ingreds.length) {
            recipeMatch.push(recipe.name);
        }

        count = 0;
    });

    console.log(recipeMatch);
}

findMatchRecipe(myIngredientsA); // doesn't find a match, since the 'Egg' is missing
findMatchRecipe(myIngredientsB); // finds a match, since all ingredients match
findMatchRecipe(myIngredientsC); // finds a match, since all ingredients match independent of the extra ones

【讨论】:

    【解决方案3】:

    谢谢它的工作,但如果我有 menuA = [carrot,pasta,cheese] 它返回 false 我希望它以任何方式与项目一起使用

    【讨论】:

    • 您回复我的解决方案了吗?我尝试了该输入并且效果很好。如果你觉得没问题,请给我点赞。我将不胜感激
    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    相关资源
    最近更新 更多