使用 jQuery .each 迭代关联数组
可能对这个问题最有影响的因素是我现在非常困。
我有一个数组,我启动它:
var cells = [];
然后我在其中放入一些值(jQuery 对象),例如:
$("td").each(function () {
var td = $(this);
cells[td.attr("id")] = td;
});
现在我的问题。这段代码:
$(cells).each(function (i) {
console.log(this) // firebug console
});
绝对没有记录任何内容。当我将关联数组更改为普通数组时,数字索引 1 替换为 ,
cells[td.attr("id")] = td;
它
cells.push(td);
工作正常。
另外,当我尝试使用 for..in 循环进行迭代时,它会按预期工作。
for (var cell in cells) {
console.log(cells[cell]);
}
这是否意味着 jQuery 的 .each 方法不接受关联数组,或者我做错了什么?
Probably the most contributing factor for this question is that I am extremely sleepy right now.
I have an array, which I initiate:
var cells = [];
Then i put some values in it (jQuery objects), for example:
$("td").each(function () {
var td = $(this);
cells[td.attr("id")] = td;
});
And now my problem. This code:
$(cells).each(function (i) {
console.log(this) // firebug console
});
logs absolutelly nothing. When i changed the associative array to a normal, number index one by substituting
cells[td.attr("id")] = td;
with
cells.push(td);
It worked correctly.
Also, when I try to iterate with the for..in loop it works as expected.
for (var cell in cells) {
console.log(cells[cell]);
}
Doeas that mean that jQuery's .each method does not accept associative arrays or am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JavaScript 没有关联数组。它有数组,也有对象,而数组恰好是对象。当您执行此操作时:
..您实际上正在执行与此等效的操作:
也就是说,您实际上正在将名为
foo
的属性添加到对象a
,不将元素添加到数组a
。从
jQuery.each()
的文档中:由于您创建了一个
Array
([]
),jQuery 会查看其length
属性,并且由于您尚未向数组添加任何元素(仅对象的属性,记住)它的长度
仍然是零,所以jQuery(正确地)什么也不做。正如其他人所指出的,您想要做的是使用例如
var cells = {};
创建一个对象。由于非数组对象没有length
属性(无论如何不是默认情况下),jQuery 会知道您确实想要迭代它的属性,而不是像在大批。JavaScript does not have associative arrays. It has Arrays and it has Objects, and arrays happen to be objects. When you do this:
..you're actually doing the equivalent of this:
That is to say you're actually adding a property called
foo
to the objecta
, not adding an element to the arraya
.From the documentation for
jQuery.each()
:Since you created an
Array
([]
) jQuery looks at itslength
property, and since you have not added any elements to the array (only properties on the object, remember) itslength
is still zero and so jQuery (correctly) does nothing.What you want to do instead, as others have noted, is create an Object using e.g.
var cells = {};
. Since a non-Array object has nolength
property (not by default, anyway) jQuery will know that you really want to iterate over its properties instead of numeric indices as in an Array.您似乎认为 Javascript 的数组是关联的,但事实并非如此。您可能正在寻找对象(或哈希):
You seem to be thinking Javascript's arrays are associative, which is not the case. You're probably looking for objects (or hashes) instead:
使用
$.each(cells, function(i) { ... })
而不是$(cells).each(function...)
$.each()
函数与$(selector).each
函数。use
$.each(cells, function(i) { ... })
instead of$(cells).each(function...)
The
$.each()
function is different from the$(selector).each
function.