Timeline for Loop through an array in JavaScript
Current License: CC BY-SA 4.0
45 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Sep 20, 2023 at 17:22 | comment | added | Chema |
ESLint complains about for..of, by default: «iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations [eslintno-restricted-syntax]». I'm tempted to forget all this madness, and stick exclusively to while ...
|
|
Jun 18, 2023 at 18:24 | comment | added | Gleichmut | This answer has in-deep explanation of this topic: stackoverflow.com/questions/9329446/… | |
May 22, 2023 at 20:09 | comment | added | horace | I'm not thrilled with forEach. for-of is much more clear semantically. Think of the next programmer: not everyone is at the same level skill-wise. Who cares about performace? So long as you are searching millions of nodes and/or subnodes in some kind of tree, or doing thousands of searches every minute. But most apps don't fit into that category. | |
Mar 21, 2023 at 6:38 | comment | added | PHP Guru | If you know that an array is sparse, for...in has the benefit of only iterating over existing keys without the need for the syncronous function callback needed by forEach. For...in is also compatible with old browsers. Don't dis for...in. As of ES5 for...in iterates over array keys first before any other properties. | |
Aug 31, 2022 at 12:15 | comment | added | Newbie... | What about performance-wise? | |
Jul 2, 2022 at 17:19 | comment | added | cprcrack |
I prefer for...of instead of forEach() because for...of doesn't use a callback and that can really help with Typescript type inference: stackoverflow.com/a/72841021/423171
|
|
May 27, 2022 at 5:42 | comment | added | Adam | I think the mentioned contra of missing "continue" is not really true, just use return inside the functions, its the equivalent. However, the missing "break" is a valid contra point. | |
May 14, 2022 at 16:32 | comment | added | Scotty Jamison | For those looking at this answer and wondering whether they should choose forEach() of for-of, I would just recommend using for-of. for-of is the newer looping syntax that entirely replaces the need for forEach. There isn't anything that for-of offers that forEach does not, but the inverse isn't true. | |
May 14, 2022 at 16:28 | comment | added | Scotty Jamison |
For reference, this is how to get the current index as you loop. I personally find this pretty straightforwards. for (const [i, x] of yourArray.entries()) { ... } .
|
|
Mar 14, 2022 at 21:41 | comment | added | Jason 'Slingshot' Miller |
What about the for(let ojb of objs) statement?
|
|
Feb 16, 2022 at 18:23 | comment | added | Alvaro Carvalho | @Vic you can, but it's not straightforward. | |
Sep 9, 2021 at 1:13 | comment | added | Vic |
con of es6 for-of : can't get the current index
|
|
Jun 15, 2021 at 11:41 | comment | added | bmaupin |
I know this answer predates async and Promises, but I feel this is worth mentioning in any conversation pertaining to modern JavaScript: "forEach does not wait for promises. Make sure you are aware of the implications while using promises (or async functions) as forEach callback." (developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…)
|
|
May 29, 2021 at 4:15 | history | rollback | user229044♦ |
Rollback to Revision 19
|
|
May 29, 2021 at 1:41 | history | edited | dthree | CC BY-SA 4.0 |
Added examples and benchmark of options.
|
May 8, 2021 at 7:37 | history | edited | Mateen Ulhaq | CC BY-SA 4.0 |
Simplify arrow function
|
May 8, 2021 at 7:31 | history | edited | Mateen Ulhaq | CC BY-SA 4.0 |
Short summary at top.
|
Nov 10, 2020 at 2:42 | review | Suggested edits | |||
Nov 10, 2020 at 11:48 | |||||
Oct 6, 2020 at 12:54 | history | edited | Peter Mortensen | CC BY-SA 4.0 |
Active reading [<https://en.wikipedia.org/wiki/Sentence_clause_structure#Run-on_sentences> (see also <https://twitter.com/PeterMortensen/status/1199839973215739907>) ]. Added some context. Expanded.
|
Jun 20, 2020 at 9:12 | history | edited | CommunityBot |
Commonmark migration
|
|
S Jun 7, 2020 at 20:56 | history | suggested | Zamomin | CC BY-SA 4.0 |
fixed grammar, removed repeated phrase
|
Jun 7, 2020 at 13:36 | review | Suggested edits | |||
S Jun 7, 2020 at 20:56 | |||||
S Jan 16, 2020 at 1:13 | history | suggested | CommunityBot | CC BY-SA 4.0 |
Fixed indenting
|
Jan 15, 2020 at 23:01 | review | Suggested edits | |||
S Jan 16, 2020 at 1:13 | |||||
Nov 22, 2019 at 13:09 | history | edited | Christian C. Salvadó | CC BY-SA 4.0 |
fix grammar errors and typos
|
S Aug 30, 2019 at 14:53 | history | suggested | Sapphire_Brick | CC BY-SA 4.0 |
proofread a little more, also clarified "off-by-one-error"
|
Aug 30, 2019 at 13:30 | review | Suggested edits | |||
S Aug 30, 2019 at 14:53 | |||||
Jul 11, 2019 at 18:54 | history | edited | Jonathan Hall | CC BY-SA 4.0 |
edited body
|
May 29, 2019 at 17:21 | history | edited | Christian C. Salvadó | CC BY-SA 4.0 |
Added executable examples, added map and reduce examples and antipatterns
|
May 16, 2019 at 17:23 | history | edited | Christian C. Salvadó | CC BY-SA 4.0 |
update answer, add ES5 forEach, ES6 for-of, pros / cons
|
S Feb 15, 2019 at 9:06 | history | suggested | Jonathan | CC BY-SA 4.0 |
internal fiddle
|
Feb 15, 2019 at 8:44 | review | Suggested edits | |||
S Feb 15, 2019 at 9:06 | |||||
Dec 4, 2018 at 22:09 | review | Suggested edits | |||
Dec 4, 2018 at 22:36 | |||||
Oct 24, 2018 at 6:46 | review | Suggested edits | |||
Oct 24, 2018 at 7:26 | |||||
Dec 26, 2017 at 15:12 | review | Suggested edits | |||
Dec 26, 2017 at 18:19 | |||||
Aug 17, 2017 at 12:31 | review | Suggested edits | |||
Aug 17, 2017 at 14:23 | |||||
Sep 26, 2014 at 21:02 | history | edited | Jonathan Hall | CC BY-SA 3.0 |
added 7 characters in body
|
Feb 24, 2014 at 20:10 | history | edited | MonkeyZeus | CC BY-SA 3.0 |
added 36 characters in body
|
S Jun 21, 2013 at 15:03 | history | suggested | Johann | CC BY-SA 3.0 |
Fix external link by pointing to http://web.archive.org/
|
Jun 21, 2013 at 15:03 | review | Suggested edits | |||
S Jun 21, 2013 at 15:03 | |||||
Apr 30, 2013 at 13:31 | history | edited | suspectus | CC BY-SA 3.0 |
deleted 2 characters in body
|
Jun 10, 2010 at 2:41 | vote | accept | Mark Szymanski | ||
Jan 6, 2012 at 18:53 | |||||
Jun 10, 2010 at 0:28 | history | edited | Christian C. Salvadó | CC BY-SA 2.5 |
added 386 characters in body
|
Jun 10, 2010 at 0:16 | history | edited | Christian C. Salvadó | CC BY-SA 2.5 |
added 588 characters in body; added 399 characters in body
|
Jun 10, 2010 at 0:07 | history | answered | Christian C. Salvadó | CC BY-SA 2.5 |