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

119
Vistas
find array of objects with array - javascript

I've been trying to do this function for two days and I'm not succeeding. I would like the function to return me the objects that satisfy the filter array. The problem is that I would like it to return only objects that satisfy all filters.

My array of filters:

const myFiltersIngredients = ['farinha', 'frango']

My array of objects:

    const myRecipes = [
  {
    name: 'torta da vovo',
    ingredients: [
      {
        ingredient: 'farinha',
        ingredientUnit: 'grama(s)',
        ingredientQuantity: 500
      },
      {
        ingredient: 'frango',
        ingredientUnit: 'grama(s)',
        ingredientQuantity: 500
      }
    ]
  },
  {
    name: 'biscoito',
    ingredients: [
      {
        ingredient: 'farinha',
        ingredientUnit: 'grama(s)',
        ingredientQuantity: 500
      },
      {
        ingredient: 'manteiga',
        ingredientUnit: 'grama(s)',
        ingredientQuantity: 500
      }
    ]
  }
]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

const myFiltersIngredients = ['farinha', 'frango'];

const myRecipes = [{
        name: 'torta da vovo',
        ingredients: [{
                ingredient: 'farinha',
                ingredientUnit: 'grama(s)',
                ingredientQuantity: 500
            },
            {
                ingredient: 'frango',
                ingredientUnit: 'grama(s)',
                ingredientQuantity: 500
            }
        ]
    },
    {
        name: 'biscoito',
        ingredients: [{
                ingredient: 'farinha',
                ingredientUnit: 'grama(s)',
                ingredientQuantity: 500
            },
            {
                ingredient: 'manteiga',
                ingredientUnit: 'grama(s)',
                ingredientQuantity: 500
            }
        ]
    }
];

const myFiltered = myRecipes.filter(({
    ingredients
}) => myFiltersIngredients.every(myfilter => ingredients.map(({
    ingredient
}) => ingredient).includes(myfilter)));

console.log( myFiltered );

about 4 years ago · Juan Pablo Isaza Denunciar

0

I hope this small demo helps point you in the right direction.

Here is a link to the .filter and .every array method documentation.

As a brief explanation:

The .filter method takes a function as an argument, and will execute the function with each object in the array. The function will return either true or false. The array returned by .filter will be all the items in the array that returned true based on the function provided.

The .every method takes a function as an argument, and returns true if every item in the array, when passed to the function argument, will returns true.

In my code I first state that I want to filter all the recipes, and return the filtered array into a variable. The function that is passed as an argument only has one line, which checks the object to make sure that every ingredient associated with that object is included in the myFiltersIngredients array.

const myFiltersIngredients = ['farinha', 'frango'];

const myRecipes = [
  {
    name: 'torta da vovo',
    ingredients: [
      {
        ingredient: 'farinha',
        ingredientUnit: 'grama(s)',
        ingredientQuantity: 500
      },
      {
        ingredient: 'frango',
        ingredientUnit: 'grama(s)',
        ingredientQuantity: 500
      }
    ]
  },
  {
    name: 'biscoito',
    ingredients: [
      {
        ingredient: 'farinha',
        ingredientUnit: 'grama(s)',
        ingredientQuantity: 500
      },
      {
        ingredient: 'manteiga',
        ingredientUnit: 'grama(s)',
        ingredientQuantity: 500
      }
    ]
  }
];

const filteredRecipes = myRecipes.filter(o => {
  return o.ingredients.every(
    ({ingredient}) => myFiltersIngredients.includes(ingredient)
  );
});

console.log(filteredRecipes);

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