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

146
Visualizações
Get to inside an array which is part of an object without a for loop, javascript

I have this code that works:

var roles = user.getRoles()
   for (var i = 0; i < roles.length; i++){
      if (Roles[i].getName()== ‘Admin’){
        --gets to this line
   }
}

However I was wondering if I could get to a TRUE/FALSE without a for/if loop like the below that isn't working for me:

 if (user.getRoles().getName().indexOf('Admin') >-1){
    -does not get to this line
 }

Gives error: user.getRoles().getName is not a function

if I print out roles, the return is: UserRole@718816d, UserRole@1ae91200, UserRole@48baf10b, UserRole@777d6d90, UserRole@34ad99fb, UserRole@6606e47c, UserRole@79ab731e, UserRole@65269056, UserRole@3d9cbf48, UserRole@6ce88983, UserRole@4e792483, UserRole@1f8ca93a

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

It seems like using .includes should work for you in this situation. You appear to be searching for a known string ('Admin')

var roles = user.getRoles()
   if (roles.includes('Admin')) {
       - gets here if true
   }
}

https://www.digitalocean.com/community/tutorials/js-array-search-methods

about 4 years ago · Juan Pablo Isaza Relatório

0

if(user.getRoles()?.find(role => role.getName() === 'admin')){
   // do stuff
}

The question mark is there to check if there is a value coming from getRoles.

Find will return null or the role.

about 4 years ago · Juan Pablo Isaza Relatório

0

So what I guess from the provided information is that the method getName() is defined on the objects inside the roles array. So user.getRoles() returns you an array on which you cannot call getName() method

What I think you can do is user.getRoles().find(el => el.getName() === 'Admin')

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