I’m not being able to get the $.each() jquery function working with string indexed arrays, any idea on whats wrong?
Example @ JSFiddle –>
http://jsfiddle.net/WKDUA/
Code:
var firstArray = [52, 33];
document.writeln("First Array:\n<ul>");
$.each(firstArray, function(key, value)
{
document.writeln('<li>[' + key + ']: ' + value + "</li>\n");
});
document.writeln("</ul>\n");
var secondArray = new Array();
secondArray['first'] = 'foo';
secondArray['second'] = 'bar';
document.writeln("Second Array:\n<ul>");
$.each(secondArray, function(key, value)
{
document.writeln('<li>[' + key + ']: ' + value + "</li>\n");
});
document.writeln("</ul>\n");
Output:
First Array:
[0]: 52
[1]: 33
Second Array:
An array is always indexed by an integer representing the position of an element.
You’re looking for an object whose properties you can access via bracket notation:
In your original code, the
$.eachblock was never being entered because you did not add any elements to the array. You did define propertiesfirstandsecondon that array and assign them values.Example: http://jsfiddle.net/ddTPu/