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

121
Visualizações
Filtering on multiple conditions using javascript filter

Before anyone calls duplicate please read it through first...

I was trying to use the filter function with multiple conditions but doesn't seem to work and alot of solutions to this roughly come out to say I can do...

const data = [
    {CatalogID: -1, SectionID: 0},
    {CatalogID: 2, SectionID: 9},
    {CatalogID: 2, SectionID: 9},
    {CatalogID: -1, SectionID: 0},
    {CatalogID: -1, SectionID: 0},
    {CatalogID: 3, SectionID: 6},
    {CatalogID: 3, SectionID: 6},
    {CatalogID: -1, SectionID: 0},
    {CatalogID: 3, SectionID: 6}
]

data.filter(f=>f.SectionID == 6 && f.SectionID == 0 && f.CatalogID == -1)

let a = data.filter(function(e) {
        return e.SectionID == 6 && e.SectionID == 0 && e.CatalogID == -1
    });
console.log(a);

But those return empty... But when I loop through the data object, like this...

let holder = [];
for(let i = 0; i < data.length; i++){
    if(data[i].SectionID === 6 || data[i].SectionID === 0 || data[i].CatalogID === -1) holder.push(data[i]);
}
console.log(holder);
holder = [];

Then it does the job, but its not pretty.

Is there something glaringly obvious that is wrong with the code I provided, that is not popping out at me?

** EDIT ** The result I am looking for is this...

{CatalogID: -1, SectionID: 0},
{CatalogID: -1, SectionID: 0},
{CatalogID: -1, SectionID: 0},
{CatalogID: 3, SectionID: 6},
{CatalogID: 3, SectionID: 6},
{CatalogID: -1, SectionID: 0},
{CatalogID: 3, SectionID: 6}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

f.sectionID = 6 && f.sectionID == 0 can't ever be true -- there can only be one sectionID value in a specific object f.

You need to use || to match different values of the same sectionID property. Then use && to combine condition with catalogID.

const data = [
    {CatalogID: -1, SectionID: 0},
    {CatalogID: 2, SectionID: 9},
    {CatalogID: 2, SectionID: 9},
    {CatalogID: -1, SectionID: 0},
    {CatalogID: -1, SectionID: 0},
    {CatalogID: 3, SectionID: 6},
    {CatalogID: 3, SectionID: 6},
    {CatalogID: -1, SectionID: 0},
    {CatalogID: 3, SectionID: 6}
]

let a = data.filter(f => f.SectionID == 6 || ( f.SectionID == 0 && f.CatalogID == -1))

console.log(a);

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