Karla Jensen
Karla Jensen

Reputation: 57

JavaScript: How to convert a for/of loop into a normal for loop with variables?

I have a code where I need to change a for/of loop into a normal for loop, but I don't know how to convert the variables. Such as if a have a

for (let sentence of sentences)

How do I change it to this structure?

for (var i = 0; i < .length; i++) 

Upvotes: 1

Views: 619

Answers (1)

svltmccc
svltmccc

Reputation: 1386

You can do this

for (let i = 0; i < sentences.length; i++) {
  console.log(sentences[i]); // it's your current sentence
}

UPD

How correctly noticed @FZs my code won't work on different types of iterables.

For transform iterator to array you can use Array.from method. I put link to MDN here

Upvotes: 2

Related Questions