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

109
Visualizações
Loop through array of objects and check conditions

I have an array of object like this

list = [
        {
          "label" : "whatever",
          "value" : "value 1"
        },
        {
          "label" : "whatever",
          "value" : "value 2"
        },
        {
          "label" : "Required scenario only",
          "value" : "value 3"
        },
        {
          "label" : "whatever",
          "value" : "value 4"
        },       
       ]

I am running for a loop with conditions, I want to execute some piece of code only for one scenario called 'Required scenario only' for the rest of all scenarios run different code for only one time, it should not execute 3 times as per loop executes

for(i=0; i< list.length; i++) {
 if(list[i].label === 'Required scenario only' ) {
    //execute some code
  } else {
    // execute code for 1 time for rest  of the labels i.e for all 'whatever' scenarios
  } 
}

How should I do it?

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

0

If you're merely checking for the existence of an object with label = "Required scenario only" then I would use Array.some():

list = [{
    label: "whatever",
    value: "value 1"
  },
  {
    label: "whatever",
    value: "value 2"
  },
  {
    label: "Required scenario only",
    value: "value 3"
  },
  {
    label: "whatever",
    value: "value 2"
  },
]
if (list.some(l => (l.label == "Required scenario only"))) {doSomething();}
else {doSomethingElse();}

function doSomething() {console.log("Do Something");}
function doSomethingElse() {console.log("Do Something Else");}

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