【问题标题】:Javascript / React Native - How can I make a cross check between two different array of objects?Javascript / React Native - 如何在两个不同的对象数组之间进行交叉检查?
【发布时间】:2021-01-25 02:49:34
【问题描述】:

我有一个应用程序,它将创建一个食谱数据库,用户将在其中输入他拥有的成分,并且该应用程序应该返回用户输入的这种组合可用的食谱。

一旦我拥有包含许多键/值信息的配方对象:

 const [recipes, setRecipes] = useState([
    { id: 1, easyOfPrepare: 'facil', author: 'www.', category: 'Salgada', type: 'Prato principal', name: 'pão de forma', key: '1', ingredients: ['sal', 'ovo', 'mantega', 'maionese', 'farinha'] },
    { id: 2, easyOfPrepare: 'facil', author: 'www.', category: 'Salgada', type: 'Entrada', name: 'macarrao ao molho branco', key: '2', ingredients: ['macarrao', 'oleo', 'creme de leite'] },
    { id: 3, easyOfPrepare: 'facil', author: 'www.', category: 'Salgada', type: 'Salada', name: 'Arroz marroquino', key: '3', ingredients: ['A', 'B', 'C'] },
    { id: 4, easyOfPrepare: 'facil', author: 'www.', category: 'Doce', type: 'Sobremesa', name: 'Bolo', key: '4', ingredients: ['farinha', 'açucar', 'etc'] },
    { id: 5, easyOfPrepare: 'facil', author: 'www.', category: 'Doce', type: 'Bolo', name: 'doce de leite', key: '5', ingredients: ['AA', 'BB', 'CC'] },
  ])

用户输入保存在另一个对象中

const [ingredients, setIngredients] = useState(['farinha', 'ovos', 'leite'])

const addIngredients = e => {
  e.preventDefault();
  setIngredients(prevIngredient => [...prevIngredient, name]);
}

一旦用户通知了预期的成分,就会调用这个添加成分。

一旦用户输入了以下组合:“sal”、“ovo”、“mantega”、“maionese”、“farinha”,我如何根据食谱过滤这些成分以定义唯一可能的成分组合是第一个?

谢谢!

【问题讨论】:

    标签: javascript node.js reactjs react-native


    【解决方案1】:

    利用数组的方法:

    const recipes = [
      { id: 1, easyOfPrepare: 'facil', author: 'www.', category: 'Salgada', type: 'Prato principal', name: 'pão de forma', key: '1', ingredients: ['sal', 'ovo', 'mantega', 'maionese', 'farinha'] },
      { id: 2, easyOfPrepare: 'facil', author: 'www.', category: 'Salgada', type: 'Entrada', name: 'macarrao ao molho branco', key: '2', ingredients: ['macarrao', 'oleo', 'creme de leite'] },
      { id: 3, easyOfPrepare: 'facil', author: 'www.', category: 'Salgada', type: 'Salada', name: 'Arroz marroquino', key: '3', ingredients: ['A', 'B', 'C'] },
      { id: 4, easyOfPrepare: 'facil', author: 'www.', category: 'Doce', type: 'Sobremesa', name: 'Bolo', key: '4', ingredients: ['farinha', 'açucar', 'etc'] },
      { id: 5, easyOfPrepare: 'facil', author: 'www.', category: 'Doce', type: 'Bolo', name: 'doce de leite', key: '5', ingredients: ['AA', 'BB', 'CC'] },
    ];
    const userInput = ['sal', 'ovo', 'mantega', 'maionese', 'farinha'];
    
    const result = recipes.filter(recipe => userInput.every(i => recipe.ingredients.includes(i)));
    
    
    console.log(result.length); // 1
    

    【讨论】:

      【解决方案2】:

      您可以使用filter 和includes 方法来实现:

      const matchedRecipes = recipes.filter(recipe => {
        for (ingredient in ingredients) {
          if (!recipe.ingredients.includes(ingredient)) {
            return false
          }
        }
        return true
      })
      

      【讨论】:

        【解决方案3】:

        您可以根据recipes过滤掉ingredients

        const filteredRecipes = recipes.filter(recipe => {
           return ingredients.every(ingredient => recipe.ingredients.includes(ingredient))
        })
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-11-06
          • 1970-01-01
          • 1970-01-01
          • 2018-05-10
          • 1970-01-01
          • 2021-11-15
          • 2020-04-02
          • 1970-01-01
          相关资源
          最近更新 更多