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

128
Visualizações
Filtering Javascript array of objects by account type

I have a javascript array that contains objects which have a few properties of which there are some booleans. I want to take the user's account type and filter out the ones that don't apply to them. I've included the filter code I have tried however it isn't working because it is filtering out if only one of the if statements come true. I understand this but am unsure how to resolve it.

Filter code

if(acc_type != 'Admin') {
      this.items = this.items.filter((item) => {
        return item.admin != true
      })
    }
    if(acc_type != 'Manager') {
      this.items = this.items.filter((item) => {
        return item.manager != true
      })
    }

Some objects from array this filter are acting on

{ header: "Management", admin: true, manage: true },
    {
        title: 'Creation Form',
        to: '/management/creation-form',
        admin: true,
        manage: true,
    },
    {
        title: 'Management',
        to: '/management',
        admin: true,
        manage: true,
    },
    { header: "Settings" },
    {
        title: 'Account',
        to: '/settings/account',
        admin: true
    },
    {
        title: 'Billing',
        to: '/billing',
        admin: true
    },
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

From how I understand:

this.items = this.items.filter(function(item){
  if(acc_type == 'Admin')
    return item.admin || !item.manager
  else if(acc_type == 'Manager')
    return item.manager || !item.admin
  else // any other acct
    return !item.admin && !item.manager
})
about 4 years ago · Juan Pablo Isaza Relatório

0

  this.items = this.items.filter((item) => {
    return acc_type != 'Admin' && item.admin != true && 
           acc_type != 'Manager' && item.manager != true
  })
about 4 years ago · Juan Pablo Isaza Relatório

0

Simply turn around your logic to make it work.

If it is an 'Admin' account, every item having admin: true should be in the resulting list. If the account is is 'Manager', everything having manage: true should be in the resulting list.

if (acc_type === 'Admin') {
  this.items = this.items.filter(item => !!item.admin);
}
else if (acc_type === 'Manager') {
  this.items = this.items.filter(item => !!item.manager);
}

According to this, for 'Admin' accounts all entries will be in the resulting list, for 'Manager' accounts there will be only the first two entries.

Don't be confused by the !! - it's only a boolean conversion. For this example it can be omitted.

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