Example: Iterate an Array as an Object

by santhosh lanka

JavaScript

/* Firebug console required */

// simple array
var myObj = [1, 2, 3, 4, 5];

alert(JSON.parse(myObj));

// alert max value of array
alert("Max is: "+myObj.max());

// iterate myObj properites - (alert only non-function, i.e. array members)
for (var key in myObj) {
    console.log("Key:" + key + " / Value: " + myObj[key] + " / Type: " + typeof myObj[key]);
    if (typeof myObj[key] != "function") {
        alert(myObj[key]);
    }
}