Thursday, June 25, 2009

Foreach equivalent in javascript

foreach function in PHP is very nice and easy to use. I was searching for equivalent in javascritp and there are two possibilities.

1. without jquery:


var new_array = new Array();
new_array[0] = 'foo';
new_array[1] = 'bar';

for (var key in new_array)
{
alert(new_array[key]);
}


2. using jquery


var new_array = new Array();
new_array[0] = 'foo';
new_array[1] = 'bar';

$.each(new_array, function(key, value)
{
alert(key + '=>' + value);
});


if you now another option using some other framework, please add it to discussion, thanks