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

100
Vistas
Filter array object by array of Boolean values dynamically

I have my array object in the following format

menuItem = [{
    "name": "MOZZARELLA DI BUFALA & TOMATOES",
    "gluten": true,
    "eggs": false,
    "dairy": true,
     ...
},...

I want to filter hundreds of menuItems by the allergen values (Boolean).

While the following code does the trick:

menuItem.filter(el => !el.gluten && !el.fish && !el.soy && !el.dairy)"

The allergens are hardcoded and repetitive. I am struggling to make the code dynamic and elegant.

I tried the following solution

menuItem.filter(el => !el[allergens])"
var allergens = ['gluten', 'fish', 'soy', 'dairy']

However, it only works correctly with one allergen value. Multiple values as in the above example do not filter anything.

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

First extract the name property, then check that the rest of the properties in the object are falsey.

menuItem.filter(
  ({ name, ...rest }) => Object.values(rest).every(val => !val)
);

Ideally, I'd prefer to restructure the input array so the allergies are in a separate property, perhaps:

menuItem = [{
    "name": "MOZZARELLA DI BUFALA & TOMATOES",
    allergens: {
        "gluten": true,
        "eggs": false,
        "dairy": true,

to make accessing them easier.

menuItem.filter(
  ({ allergens }) => Object.values(allergens).every(val => !val)
);
about 4 years ago · Santiago Trujillo Denunciar

0

You could use .some or .every, whichever match your case

const allergens = ['gluten', 'fish', 'soy', 'dairy']

const res = menuItem.filter(
  (el) => !allergens.every((allergen) => el[allergen])
);
// equivalent to `menuItem.filter(el => !el.gluten && !el.fish && !el.soy && !el.dairy)`

const menuItem = [
  {
    name: 'dish A',
    gluten: true,
    fish: true,
    soy: true,
    dairy: true,
  },
  {
    name: 'dish B',
    gluten: true,
    fish: true,
    soy: false,
    dairy: true,
  },
];

const allergens = ['gluten', 'fish', 'soy', 'dairy'];

const res = menuItem.filter(
  (el) => !allergens.every((allergen) => el[allergen])
);

console.log(res);

about 4 years ago · Santiago Trujillo 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