Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

94
Visualizações
Matching An Entire Array To Part Of Another In React

I am currently writing an app that lets users check boxes, and help them decide what drinks to make with whatever ingredients they have. At the moment, I have it set up so the user can go through a list of ingredients, and check off which ones they have. Then after hitting submit, the app will go through all the ingredients of all the drinks, and if a drink recipe contains all the ingredients the user has, it will return that drink.

The problem is, I want the user to be able to put in multiple ingredients, but not have the app only return the recipe if it has ALL of the ingredients. Example: The user puts in they have vodka, rum, and limes, I want the app to return the recipes that require (Vokda, limes), (Rum, Limes), (Rum, Vodka), (Rum, Vodka, Limes), (Rum), (Limes), (Vodka).

Here's what I have so far.

import React from 'react';
import Data from '../Data'

function HavedRecipeCards(props) {

    let totalDrinks = Data.drinks
    let havedIngredients = props.selectedIngredients
    // havedIngredients is represented with a normal array
    // Ex: ['vermouth', 'limes']

    let checkRecipes = () => {
        if (havedIngredients.length > 0) {
            for (let i = 0; i < totalDrinks.length; i++ ) {
                let drinkIng = totalDrinks[i].ingredients

                for (let p = 0; p <= havedIngredients.length; p++) {
                    for (let o = 0; o < drinkIng.length; o ++) {
                        if (havedIngredients[p] == drinkIng[o]) {

                        }
                    }
                }
            }
        }
    }

    return (
        <div>
            <input type="submit" value="Submit" onClick={checkRecipes}/>
        </div>
    )
}

export default HavedRecipeCards;

I feel there's a better way to do this without looping through the entirety of the drink list, drink ingredients list, and haved ingredients list. Any help is appreciated, thank you.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can filter the total drinks by those in which every ingredient is part of the havedIngredients.

const totalDrinks = [['Vodka','Limes'], ['Rum','Limes'], ['Vodka', 'Rum','Limes'], ['Vodka','Vermouth']];
const havedIngredients = ['Vodka', 'Rum', 'Limes'];

const possibleDrinks = totalDrinks.filter(ingredients => 
  ingredients.every(ingredient => havedIngredients.includes(ingredient))
);

alert(possibleDrinks.join("\n"));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda