Iterating through JS arrays
Posted: Tue Sep 20, 2005 1:33 pm
Is there a way to iterate through arrays in JavaScript like the while (list($key,$value) = each($arrayname)) on PHP?
TIA
TIA
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
// emulation for IE and whatnot
if(!Array.prototype.forEach) {
Array.prototype.forEach = function(callback, thisPtr) {
for(var i=0, l=this.length; i<l; i++) {
if(thisPtr) {
callback.call(thisPtr, this[i], i, this);
} else {
callback(this[i], i, this);
}
}
}
}
var theArray = ['first', 'second', 'third'];
theArray.forEach(function(elt, index) {
// do something
});Code: Select all
var theArray = ['first', 'second', 'third'];
theArray.forEach(function(elt, index) {
// do something
});Missed the linkJcart wrote:Read the link he gave you.. explains it well