Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!