Home > Net >  find array of objects with array - javascript
find array of objects with array - javascript

Time:12-14

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

CodePudding user response:

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

CodePudding user response:

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

  • Related