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

164
Vistas
How to use Key in for of loop of JavaScript

I am using for of loop of JavaScript. My code is like below.

for (let [key, value] of data) {
   if(key == 0) {
     // do something.
   } else {
   // do something.
  }

I am getting below error.

Uncaught (in promise) TypeError: data[Symbol.iterator]().next().value is not iterable

How to use Key in for of loop of JavaScript ?

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

0

If your data is an object you could do

const data = {
a: 1,
b: 2,
c : 3
}

for(let [key, value] of Object.entries(data)){
 console.log(`key: ${key} and value: ${value}`)
}

As @Maik Lowrey noted the problem seems to be a different

Looks like data is a promise (or an array of promises)

int the first case you just have to call .then

const data = Promise.resolve({
    a: 1,
    b: 2,
    c: 3
  })


data.then(d => {
  for (let [key, value] of Object.entries(d)) {
    console.log(`key: ${key} and value: ${value}`)
  }
})

const data2 = [4, 5, 6].map(n => Promise.resolve(n))


Promise.all(data2).then(d => {
  for (let [key, value] of Object.entries(d)) {
    console.log(`key: ${key} and value: ${value}`)
  }
})

about 4 years ago · Juan Pablo Isaza Denunciar

0

Your data is still a Promise at the time of the loop. Apparently you are getting data from an API or something similar. This means you have to wait until the Promise is resolved. You can work with async await or loop inside then().

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