Skip to content Skip to sidebar Skip to footer

Array Displays Zero Length And No Value With Each()

I am more familiar with PHP than with JQuery and kind of stuck on arrays. I have read just about all the posts on the forum on this subject but can't get it to work. I have what I

Solution 1:

That is not a valid JavaScript array. You want to use an object:

var myArr = {'option-4': '3', 'option-1': '8', 'option-3': '0' };

You can then iterate over all keys using a for .. in:

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

This is equivalent to a associative array in PHP. Note that you need to use the explicit key to access an element, you cannot use an index, eg myArr[0].

Solution 2:

If you want to use jQuery:

$.each(myArr , function(key, val) { 
  alert(key + ": " + val); 
});

Post a Comment for "Array Displays Zero Length And No Value With Each()"