Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!