tags:

views:

67

answers:

5

i am tring to loop through an array i have the following code

 var substr = currnt_image_list.split(','); //This will split up 21,32,234,223,

Am trying to get all the data out of the array. Can some one lead me in the right path please.

A: 

Use the each() function of jQuery.

Here is an example:

$.each(currnt_image_list.split(','), function(index, value) { 
  alert(index + ': ' + value); 
});
romaintaz
A: 

You can use a for loop:

var things = currnt_image_list.split(','); 
for(var i = 0; i < things.length; i++) {
    //Do things with things[i]
}
SLaks
+3  A: 

No need for jquery here just a for loop works:

var substr = currnt_image_list.split(',');
for(var i=0; i< substr.length; i++) {
  alert(substr[i]);
}
Nick Craver
+1  A: 
$.each(substr , function(i, val) { 
  alert(substr [i]); 
});

jquery each

skyfoot
+7  A: 

Two options:

Generic loop:

var i;
for (i = 0; i < substr.length; ++i) {
    // do something with `substr[i]`
}

Advantages: Straight-forward, no dependency on jQuery, easy to understand, no issues with preserving the meaning of this within the body of the loop, no unnecessary overhead of function calls (e.g., faster, though you have to do a lot of loops before speed becomes an issue).

jQuery.each:

jQuery.each(substr, function(index, item) {
    // do something with `item`
}

(Link to docs)

Advantages: Declarative, can use a prebuilt function for the iterator if you have one handy, if your loop body is complex the scoping of a function call is sometimes useful, no need for an i variable in your containing scope.

What NOT to do:

Don't use for..in for this (or if you do, do it with proper safeguards). You'll see people saying to (in fact, briefly there was an answer here saying that), but for..in does not do what many people think it does (it does something even more useful!). Specifically, for..in loops through the enumerable property names of an object (not the indexes of an array). Since arrays are objects, and their only enumerable properties by default are the indexes, it mostly seems to sort of work in a bland deployment. But it's not a safe assumption that you can just use it for that. Here's an exploration: http://jsbin.com/exohi/3

I should soften the "don't" above. If you're dealing with sparse arrays (e.g., the array has 15 elements in total but their indexes are strewn across the range 0 to 150,000 for some reason, and so the length is 150,001), and if you use appropriate safeguards like hasOwnProperty and checking the property name is really numeric (see link above), for..in can be a perfectly reasonable way to avoid lots of unnecessary loops, since only the populated indexes will be enumerated.

T.J. Crowder
using `.each()` or `for...in` to loop over an array is in general a bad idea. It's just like ages slower than using `for` or `while`. Using a `for loop` it's even a great idea to cache the `length` property before looping. `for (var i = 0, len = substr.length; i < len; ...`
jAndy
@jAndy: I believe I did mention speed being an advantage of the first one. Re caching the length, it would have to be a **REALLY** big array to be worth the overhead, but fair 'nuff.
T.J. Crowder
+1 great and detailed answer
Steve Claridge