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

117
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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