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

125
Visualizações
Angular iterate an array with two conditions

I have this array and these values:

levels$: Observable<any[]> = [
    {
       name: 'first level primary school'     
    }, 
    {
       name: 'second level primary school'     
    },
    {
      name: 'first level secondary school'     
    }
]

const isPrimarySchool = false
const isSecondarySchool = false

And I want to iterate the array and find if there is a string with the word 'primary' or 'secondary' (it could be both), and turn the value of isPrimarySchool or isSecondarySchool to true, or both if it's the case.

The method that I found to resolve this, is this one:

this.levels$.subscribe(levels =>
      levels.forEach(level => {
        if (level.name.toLowerCase().includes('primary') {
          this.isPrimarySchool = true
        }
        if (level.name.toLowerCase().includes('secondary')) {
          this.isSecondarySchool = true
        }
      })
    )

Is there a better way to resolve the two 'if' parts, using lodash or normal javascript?

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

0

this.levels$.subscribe(levels =>{
this.isPrimarySchool =levels.some(level => level.name.toLowerCase().includes('primary'));
this.isSecondarySchool = levels.some(level => level.name.toLowerCase().includes('secondary'));
    })
about 4 years ago · Juan Pablo Isaza Relatório

0

You could use If/Else instead of 2 Ifs, and use a 3rd else.

Remember that using If/elsif/else will be less costly than adding 2 If's.

Or, you could ternary operators but that's most likely not a better approach.

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