简体   繁体   中英

Iterating an associative array (key/value pair) in javascript with jQuery, where the values are jQuery objects

I have an associative array which has a string-based key, and a jQuery object as the value. That object may contain more than one jQuery objects itself. Since this is an associated array, I should be able to iterate through it with code like the following.

$.each(array_name, function(key, value){
  // Code to actually do something here
}

Unfortunately, it doesn't actually iterate through the array at all. I notice that the length of the array is 0, despite containing objects. Short of making this an indexed array, and maintaining the index myself, I don't see what else to do. What am I doing wrong?

I have created a jsfiddle to illustrate what I am doing.

An object should be declared as {}

var test = {}

Changing that seems to fix the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM