Results 1 to 3 of 3

Thread: Javascript ForEach loop equivalent

  1. #1
    Join Date
    Nov 2008
    Posts
    43

    Javascript ForEach loop equivalent

    Good afternoon,
    Is there any equivalent to ForEach loop in javascript? i want to setup the foreach type structure in Javascript, I', just searching the any other equivalent way. Do you any?

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Javascript ForEach loop equivalent

    Following is the example of ForEach loop equivalent in Javascript:


    var names = ["abuzar","shyam","raj"];

    for ( var n in names )
    {
    alert( names[n] );
    }

    In the above code "n " variable is our iterator and by the use of "in" the "for" loop actually loops through all elements in the Array for us. Using this you no longer have to worry about the length of the array.

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Javascript ForEach loop equivalent

    There is also way for circumvent looping through the prototyped methods.

    Code:
    for (var i in arrItems) if (arrItems.hasOwnProperty(i)) {
    alert(i + ' => ' + arrItems[i]);
    }
    or you acan also just use the JS array method : https://developer.mozilla.org/En/Cor.../Array/ForEach

    Code:
    arrItems.forEach(function(v,k) { alert(k + ' => ' + v); });

Similar Threads

  1. C# Equivalent Of Javascript's Location.replace() Function?
    By ASHER in forum Software Development
    Replies: 6
    Last Post: 16-05-2010, 01:20 AM
  2. JavaScript equivalent of href="mailto:emialaddress@gmail.com"
    By Rastogi B in forum Software Development
    Replies: 6
    Last Post: 12-05-2010, 09:55 AM
  3. c# function equivalent to gettime function in javascript
    By Omaar in forum Software Development
    Replies: 4
    Last Post: 10-03-2010, 10:44 PM
  4. Foreach statement of C sharp
    By Ram Bharose in forum Software Development
    Replies: 5
    Last Post: 19-01-2010, 08:42 AM
  5. Problem with a foreach loop in PHP
    By JiJi in forum Software Development
    Replies: 2
    Last Post: 20-11-2008, 05:57 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,485,801.00763 seconds with 17 queries