简体   繁体   English

使用jQuery迭代javascript中的关联数组(键/值对),其中值是jQuery对象

[英]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. 我有一个关联数组,该数组具有一个基于字符串的键和一个jQuery对象作为值。 That object may contain more than one jQuery objects itself. 该对象本身可能包含多个jQuery对象。 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. 我注意到,尽管包含对象,但数组的长度为0。 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. 我创建了一个jsfiddle来说明我在做什么。

An object should be declared as {} 对象应声明为{}

var test = {}

Changing that seems to fix the issue. 更改它似乎可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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