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

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

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