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

148
Visualizações
Loop through arrayObjects with condition Javascript

I found a nice example on here showing how to look through arrayObjects with a condition but I have a question.

As is stands its console.logging every time it the condition is false. Is it possible to only console.log once when its finished looping through everything.

var arrayObjects = [{
    "building": "A",
    "status": "good"
  },
  {
    "building": "B",
    "status": "horrible"
  }
];

for (var i = 0; i < arrayObjects.length; i++) {
  console.log(arrayObjects[i]);

  for (key in arrayObjects[i]) {

    if (key == "status" && arrayObjects[i][key] == "good") {

      console.log(key + "->" + arrayObjects[i][key]);
    } else {
      console.log("nothing found");
    }
  }
}

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

0

I'm assuming that you want it to print Nothing found when nothing's really found, not even a single thing..

Then, you can try this.


var arrayObjects = [{"building":"A", "status":"good"}, 
{"building":"B","status":"horrible"}];

var isFound = false;

 for (var i=0; i< arrayObjects.length; i++) {
  console.log(arrayObjects[i]);

   for(key in arrayObjects[i]) {

  if (key == "status" && arrayObjects[i][key] == "good") {
      isFound = true
      console.log(key + "->" + arrayObjects[i][key]);
  }
 }
}

if (isFound === false){
      console.log("nothing found");
  }
about 4 years ago · Juan Pablo Isaza Relatório

0

Simply use .length with if condition.

var arrayObjects = [{
    "building": "A",
    "status": "good"
  },
  {
    "building": "B",
    "status": "horrible"
  }
];

for (var i = 0; i < arrayObjects.length; i++) {
  console.log(arrayObjects[i]);

  if( i === arrayObjects.length-1 ) {
      console.log("nothing found");
  }
}

about 4 years ago · Juan Pablo Isaza Relatório

0

If you're willing to refactor you code, you can save on time complexity by using just one loop with Array.reduce()

var arrayObjects = [{
    "building": "A",
    "status": "good"
  },
  {
    "building": "B",
    "status": "horrible"
  }
];
const foundKeys = arrayObjects.reduce((bool, key) => {
    console.log(key)
    if (key.status === "good") {
      console.log("status ->", key.status);
      bool = true
    }
    return bool 
}, false)

if (!foundKeys) {
    console.log("Nothing found")
}

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