Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 04-08-2009
Member
 
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?
Reply With Quote
  #2  
Old 04-08-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
Re: Javascript ForEach loop equivalent

Following is the example of ForEach loop equivalent in javascript:


Quote:
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.
Reply With Quote
  #3  
Old 04-08-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
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); });
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 02:35 AM.