Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

171
Vistas
How to embed an if statement in a "for of" loop in JS

I have a genuine quetion. Given this simple JS code:

for (let x of array) {
   if (x != array[0]) {
       // do stuff
   }
}

Is there a way to embed the "if" statement directly into the for loop?

I tried something like this:

for (let x of array if x != array[0] {
   // do stuff    
}

And:

for (let x != array[0] of array) {
   // do stuff
}

But both of them didn't work. Let me know if you can find a solution!

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can filter the array with the condition and iterate through that result:

const array = [1, 2, 3]

for(let x of array.filter(e => e != array[0])){
    console.log(x)
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

You cant pass a boolean in a "for of" or "for in" loops , the only way of do a comparison for an iteration of a loop is doing :

let array = ["37","728"];

for (let x = 0 ; ((x < array.length) && array[x] == "37") ; x++){
  console.log(array[x]);
}

And it will break the for loop if the second condition pass to true

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you specifically only want to do stuff on the 2nd - nth element, you can just use a for loop and start at 1 instead of 0.

for (let i = 1; i < arr.length; i++) {
  // do stuff
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda