Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Converting .forEach to for Loop

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How would I convert each of these from .forEach to for Loop?

These would be the simple for Loops.

Javascript For Loops in the ECMA Standard
Simple For Loop



(1)


(2)


(3)


(4)


(5)

 
Sheriff
Posts: 67579
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why? Functional approaches to JS are vastly preferred over imperative approaches.

But to answer your question: remember that forEach operates on any array (or array-like construct) and passes each instance to its lambda, so just treat the array as you normally would in a for loop, and obtain the current value with indexing (feels dirty).
 
Saloon Keeper
Posts: 24174
165
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "for" loop dates back to at least 1954 with the invention of the FORTRAN programming language. The "forEach" construct is much more recent, gaining prominence with OOP languages and especially with collections. It became popular because it's much tidier and in many cases having an actual visible loop counter wasn't necessary anyway. A side benefit being that you can potentially parallelize a forEach easier than you can a "for".

I'm glad I have a choice of constructs these days, but when either is possible, I prefer to go with forEach - it's simply cleaner.
 
Bear Bibeault
Sheriff
Posts: 67579
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:in many cases having an actual visible loop counter wasn't necessary anyway.


And in the event the counter is desired, it is passed as the 2nd argument to the forEach lambda.
 
Yup, yup, yup. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic