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?
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?
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.
There is also way for circumvent looping through the prototyped methods.
or you acan also just use the JS array method : https://developer.mozilla.org/En/Cor.../Array/ForEachCode:for (var i in arrItems) if (arrItems.hasOwnProperty(i)) { alert(i + ' => ' + arrItems[i]); }
Code:arrItems.forEach(function(v,k) { alert(k + ' => ' + v); });
Bookmarks