In Java you can use a for()
loop to go through objects in an array like so:
String[] myStringArray = {"Hello","World"};
for(String s : myStringArray)
{
//Do something
}
can you do the same in JavaScript?
In Java you can use a
can you do the same in JavaScript?
| ||||
feedback
|
Use a sequential
@zipcodeman suggests the use of the It shouldn't be used for array-like objects because:
The second point is can give you a lot of problems, for example, if you extend the For example:
The above code will alert, "a", "b", "c" and "foo!". That be particularly a problem if you use some library that relies heavily on native prototypes augmention (such as MooTools for example). The
In the above example the I would recommend you to read the following article: | |||||||||||||||||||||
feedback
|
You can use
The general syntax is:
The return value of
And now x is EDIT:I must clarify: this concept is from the functional paradigm. You don't have to write the function inline; one might do so as a first sketch, but you could then extract it into its own function.
which would be sort-of equivalent to:
except you don't get the | |||||||||||||||||||||
feedback
|
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. | |||||||||||||
feedback
|
To directly answer the question: no, you can't. JavaScript does not have a concise syntax for iterating over Arrays. At least, not universally. If you want code that is both efficient and universal (that is, it runs in all browsers and other js interpreters), you need to use an explicit counting loop. The safest version, which handles sparse arrays properly, is this:
Assigning the length value to the local variable (as opposed to including the full If you're willing to restrict yourself to a subset of available JavaScript engines, then you can use the enhanced looping method
The performance of The | ||||
feedback
|
Opera, Safari, Firefox and Chrome now all share a set of enhanced Array methods for optimizing many common loops. You may not need all of them, but they can be very useful, or would be if every browser supported them. The mozilla labs published the algorithms they and webkit both use, so that you can add them yourself. filter returns an array of items that satisfy some condition or test. every returns true if every array member passes the test. some returns true if any pass the test. forEach runs a function on each array member and doesn't return anything. map is like forEach, but it returns an array of the results of the operation for each element. These methods all take a function for their first argument, and have an optional second argument, which is an object whose scope you want to impose on the array members as they loop through the function. Ignore it until you need it. indexOf and lastIndexOf find the appropriate position of the first or last element that matches its argument exactly.
| ||||
feedback
|
Can't beleive no one mentioned using the while loop...
logs: 'one','two','three' and for the reverse order, an even more efficient loop
logs: 'three','two','one' or the classical for loop
logs: 'one','two','three' reference: http://www.sitepoint.com/google-closure-how-not-to-write-javascript/ | |||||||
feedback
|
ok, seriously do not use the method that includes looking up the length on each iteration.
or if you really want to get the id and have a really classical for loop:
| |||||||
feedback
|
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:
| |||
feedback
|
There's a method to iterate over only own object properties, not including prototype's ones:
but it still will iterate over custom-defined properties. In javascript any custom property could be assigned to any object including array. If one wants to iterate over sparsed array, | |||
feedback
|
It's not 100% identical, but similar:
| |||
feedback
|
If you want a terse way to write a fast loop and you can iterate in reverse:
This has the benefit of caching the length (similar to There are even some times when you ought to iterate in reverse, such as when iterating over a live NodeList where you plan on removing items from the DOM during iteration. | |||
feedback
|
for-in
loop enumerates object properties, without an specific order, and it also enumerates inherited properties... for iterating over arrays sequential loops are always recommended... – CMS Jun 10 '10 at 0:38