In Java you can use a for
loop to traverse objects in an array as follows:
String[] myStringArray = {"Hello", "World"};
for (String s : myStringArray)
{
// Do something
}
Can you do the same in JavaScript?
In Java you can use a for
loop to traverse objects in an array as follows:
String[] myStringArray = {"Hello", "World"};
for (String s : myStringArray)
{
// Do something
}
Can you do the same in JavaScript?
You can use
map
, which is a functional programming technique that's also available in other languages like Python and Haskell.The general syntax is:
In general
func
would take one parameter, which is an item of the array. But in the case of JavaScript, it can take a second parameter which is the item's index, and a third parameter which is the array itself.The return value of
array.map
is another array, so you can use it like this:And now x is
[10,20,30,40]
.You don't have to write the function inline. It could be a separate function.
which would be sort-of equivalent to:
Except you don't get the
new_list
.In JavaScript it's not advisable to loop through an Array with a for-in loop, but it's better using a for loop such as:
It's optimized as well ("caching" the array length). If you'd like to learn more, read my post on the subject.
I did not yet see this variation, which I personally like the best:
Given an array:
You can loop over it without ever accessing the length property:
See this JsFiddle demonstrating that: http://jsfiddle.net/prvzk/
This only works for arrays that are not sparse. Meaning that there actually is a value at each index in the array. However, I found that in practice I hardly ever use sparse arrays in Javascript... In such cases it's usually a lot easier to use an object as a map/hashtable. If you do have a sparse array, and want to loop over 0 .. length-1, you need the for (var i=0; i<someArray.length; ++i) construct, but you still need an if inside the loop to check whether the element at the current index is actually defined.
Also, as CMS mentions in a comment below, you can only use this on arrays that don't contain any falsish values. The array of strings from the example works, but if you have empty strings, or numbers that are 0 or NaN, etc. the loop will break off prematurely. Again in practice this is hardly ever a problem for me, but it is something to keep in mind, which makes this a loop to think about before you use it... That may disqualify it for some people :)
What I like about this loop is:
The reason this works is that the array specification mandates that when you read an item from an index >= the array's length, it will return undefined. When you write to such a location it will actually update the length.
For me, this construct most closely emulates the Java 5 syntax that I love:
... with the added benefit of also knowing about the current index inside the loop
I would thoroughly recommend making use of the underscore.js library. It provides you with various functions that you can use to iterate over arrays/collections.
For instance:
It's not 100% identical, but similar:
for (var s of myStringArray) {
(Directly answering your question: now you can!)
Most other answers are right, but they do not mention (as of this writing) that ECMA Script
62015 is bringing a new mechanism for doing iteration, thefor..of
loop.This new syntax is the most elegant way to iterate an array in javascript (as long you don't need the iteration index), but it is not yet widely supported by the browsers.
It currently works with Firefox 13+, Chrome 37+ and it does not natively work with other browsers (see browser compatibility below). Luckily we have JS compilers (such as Babel) that allow us to use next-generation features today.
It also works on Node (I tested it on version 0.12.0).
Iterating an array
Iterating an array of objects
Iterating a generator:
(example extracted from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of)
Compatibility table: http://kangax.github.io/es5-compat-table/es6/#For..of loops
Spec: http://wiki.ecmascript.org/doku.php?id=harmony:iterators
}