foreach() loop in javascript...?!?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: foreach() loop in javascript...?!?

  1. #1
    Join Date
    Jul 2004
    Location
    Holy Land
    Posts
    306

    foreach() loop in javascript...?!?

    Is there some kind of loop in Javascript that's equivalent to the foreach() loop in PHP???


    PHP Code:
    foreach ( $AREA as $AreadID => $AreaName) {
            
    // do something //

    Rate this post if you found it useful!
    10X, gilly914

  2. #2
    Join Date
    May 2004
    Location
    MN / NJ, United States
    Posts
    768

    Re: foreach() loop in javascript...?!?

    I don't recall how to do it. In JavaScript, I'm not sure that one can have arrays with indices other than incrementing numbers; I would say no, but that is just a guess.
    Last edited by Dr. Script; January 24th, 2006 at 04:43 PM.
    *9-11-01* Never Forget; Never Forgive; Never Relent!
    "If we ever forget that we're one nation under God, then we will be a nation gone under." - Ronald Reagan

  3. #3
    Join Date
    Jul 2004
    Location
    Holy Land
    Posts
    306

    Re: foreach() loop in javascript...?!?

    I don't think I understand what you mean...?

    What is the code you gave me supposed to do? and how can it help me, in my case?
    Rate this post if you found it useful!
    10X, gilly914

  4. #4
    Join Date
    May 2004
    Location
    MN / NJ, United States
    Posts
    768

    Re: foreach() loop in javascript...?!?

    PHP can have different keys or indices for each element. Terefore, you could something like: $arr['car'];, if you defined in your array "car" => "Honda" .

    For a JS solution, see below.
    Last edited by Dr. Script; January 24th, 2006 at 04:42 PM.
    *9-11-01* Never Forget; Never Forgive; Never Relent!
    "If we ever forget that we're one nation under God, then we will be a nation gone under." - Ronald Reagan

  5. #5
    Join Date
    Jul 2004
    Location
    Holy Land
    Posts
    306

    Re: foreach() loop in javascript...?!?

    well thanks anyway...

    I'm sure this will help me some time in the future...
    Rate this post if you found it useful!
    10X, gilly914

  6. #6
    Join Date
    May 2004
    Location
    Germany
    Posts
    655

    Re: foreach() loop in javascript...?!?

    well... you can indeed have arrays with associative indices in javascript, means not only numbers. you can even have multi-dimensional arrays...

    Code:
    <script type="text/javascript">
        var aCharts = new Array();
        aCharts['classes']       = new Array( 'notselected', 'selected' );
        aCharts['base_url']      = '***************';
        
        aCharts['images']        = new Array();
        aCharts['images'][0]     = '1D';
        aCharts['images'][1]     = '3M';
        aCharts['images'][2]     = '6M';
        aCharts['images'][3]     = '1Y';
        aCharts['images'][4]     = '3Y';
    </script>
    well and for the foreach...
    you have to use a for loop in this way:
    Code:
        for( var sIndex in aArray ) {
            var sValue = aArray[sIndex];
    
            ....
        }
    To see the whole thing in action i'm attaching a sample file with just dumps the aCharts array.
    Attached Files Attached Files
    there are 10 kinds of people. those who understand binary and those who don't...

    rate a post if you find it usefull, thx
    check out my Firefox/Mozilla Extension: http://urlparams.blogwart.com/

  7. #7
    Join Date
    Jul 2004
    Location
    Holy Land
    Posts
    306

    Re: foreach() loop in javascript...?!?

    Gee...

    Thanks for the examples and explanations!!!

    Rate this post if you found it useful!
    10X, gilly914

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center