Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

95
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda