Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3273470
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:56:07+00:00 2026-05-17T18:56:07+00:00

I am trying to loop through an array. I have the following code: var

  • 0

I am trying to loop through an array. I have the following code:

 var currnt_image_list= '21,32,234,223';
 var substr = currnt_image_list.split(','); // array here

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:56:08+00:00Added an answer on May 17, 2026 at 6:56 pm

    (Update: My other answer here lays out the non-jQuery options much more thoroughly. The third option below, jQuery.each, isn’t in it though.)


    Four options:

    Generic loop:

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

    or in ES2015+:

    for (let 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., in theory faster, though in fact you’d have to have so many elements that the odds are you’d have other problems; details).

    ES5’s forEach:

    As of ECMAScript5, arrays have a forEach function on them which makes it easy to loop through the array:

    substr.forEach(function(item) {
        // do something with `item`
    });
    

    Link to docs

    (Note: There are lots of other functions, not just forEach; see the answer referenced above for details.)

    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.

    Disadvantages: If you’re using this in the containing code and you want to use this within your forEach callback, you have to either A) Stick it in a variable so you can use it within the function, B) Pass it as a second argument to forEach so forEach sets it as this during the callback, or C) Use an ES2015+ arrow function, which closes over this. If you don’t do one of those things, in the callback this will be undefined (in strict mode) or the global object (window) in loose mode. There used to be a second disadvantage that forEach wasn’t universally supported, but here in 2018, the only browser you’re going to run into that doesn’t have forEach is IE8 (and it can’t be properly polyfilled there, either).

    ES2015+’s for-of:

    for (const s of substr) { // Or `let` if you want to modify it in the loop body
        // do something with `s`
    }
    

    See the answer linked at the top of this answer for details on how that works.

    Advantages: Simple, straightforward, offers a contained-scope variable (or constant, in the above) for the entry from the array.

    Disadvantages: Not supported in any version of IE.

    jQuery.each:

    jQuery.each(substr, function(index, item) {
        // do something with `item` (or `this` is also `item` if you like)
    });
    

    (Link to docs)

    Advantages: All of the same advantages as forEach, plus you know it’s there since you’re using jQuery.

    Disadvantages: If you’re using this in the containing code, you have to stick it in a variable so you can use it within the function, since this means something else within the function.

    You can avoid the this thing though, by either using $.proxy:

    jQuery.each(substr, $.proxy(function(index, item) {
        // do something with `item` (`this` is the same as it was outside)
    }, this));
    

    …or Function#bind:

    jQuery.each(substr, function(index, item) {
        // do something with `item` (`this` is the same as it was outside)
    }.bind(this));
    

    …or in ES2015 (“ES6”), an arrow function:

    jQuery.each(substr, (index, item) => {
        // do something with `item` (`this` is the same as it was outside)
    });
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having problems with my JScript code. I am trying to loop through
I'm trying to loop through items of a checkbox list. If it's checked, I
I'm trying to solve the 3n+1 problem and I have a for loop that
I'm trying to animate Visio objects with a loop, such as: For reposition =
I'm trying to update a hashtable in a loop but getting an error: System.InvalidOperationException:
I'm trying to use a break statement in a for loop, but since I'm
Trying to get my css / C# functions to look like this: body {
Trying to setup an SSH server on Windows Server 2003. What are some good
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Trying to make a make generic select control that I can dynamically add elements

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.