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

66
Vistas
filter array of objects if properties are in another array and values are true

The following is a sample that yields the desired output.

const data = [
                { itemID: '300', status: 'active', inventoryFlag: true, certifiedFlag: false, donateFlag: true },
                { itemID: '400', status: 'inactive', inventoryFlag: true, certifiedFlag: true, donateFlag: false },
                { itemID: '500', status: 'active', inventoryFlag: false, certifiedFlag: false, donateFlag: false },
                { itemID: '600', status: 'active', inventoryFlag: false, certifiedFlag: true, donateFlag: true },
                { itemID: '700', status: 'inactive', inventoryFlag: false, certifiedFlag: true, donateFlag: false }
            ];
           
document.getElementById("data").innerText = JSON.stringify(data.filter(o => o.inventoryFlag || o.donateFlag));
<textarea id="data" rows="50" cols="100"></textarea>

I'm trying to find a way to list the filter-by flags in a separate array and then use that in the .filter() somehow to get the same result set as the above code.

For example, here are the flags to use for filtering AND at least one of the values must be true in the object to be included in the final dataset -

const flags = ['inventoryFlag', 'donateFlag'];

As a start, I tried this but it didn't do anything:

const filteredData = data.filter(o => flags.includes(Object.keys(o)));
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Inside of the filter, use the some method to detect if any of the property keys of an entry, that are contained in the flags array, have the a value of true.

const data = [
  { itemID: '300', status: 'active', inventoryFlag: true, certifiedFlag: false, donateFlag: true },
  { itemID: '400', status: 'inactive', inventoryFlag: true, certifiedFlag: true, donateFlag: false },
  { itemID: '500', status: 'active', inventoryFlag: false, certifiedFlag: false, donateFlag: false },
  { itemID: '600', status: 'active', inventoryFlag: false, certifiedFlag: true, donateFlag: true },
  { itemID: '700', status: 'inactive', inventoryFlag: false, certifiedFlag: true, donateFlag: false }
];

const flags = ['inventoryFlag', 'donateFlag'];

const result = data.filter(entry => 
  flags.some(flag => entry[flag] === true)
);

console.log(result);

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