javascript 使用 jQuery .each 迭代关联数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7671727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterating an associative array with jQuery .each
提问by Przemek
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:
然后我将一些值放入其中(jQuery 对象),例如:
$("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..in 循环进行迭代时,它按预期工作。
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?
这是否意味着 jQuery 的 .each 方法不接受关联数组,或者我做错了什么?
回答by Jordan Running
JavaScript does not have associative arrays. It has Arrays and it has Objects, and arrays happen to be objects. When you do this:
JavaScript 没有关联数组。它有数组,也有对象,而数组恰好是对象。当你这样做时:
var a = [];
a['foo'] = 'bar';
..you're actually doing the equivalent of this:
..你实际上是在做这样的事情:
var a = [];
a.foo = 'bar';
// ^--- property of object 'a'
That is to say you're actually adding a propertycalled foo
to the objecta
, notadding an elementto the arraya
.
也就是说你实际上添加一个属性叫foo
的对象a
,不添加元素的数组a
。
From the documentation for jQuery.each()
:
从文档中jQuery.each()
:
Arrays and array-like objects with a
length
property (such as a function'sarguments
object) are iterated by numeric index, from0
tolength-1
. Other objects are iterated via their named properties.
具有
length
属性的数组和类数组对象(例如函数的arguments
对象)通过数字索引进行迭代,从0
到length-1
。其他对象通过它们的命名属性进行迭代。
Since you created an Array
([]
) jQuery looks at its length
property, and since you have not added any elements to the array (only propertieson the object, remember) its length
is still zero and so jQuery (correctly) does nothing.
由于您创建了一个Array
( []
) jQuery 查看其length
属性,并且由于您没有向数组添加任何元素(请记住,只有对象上的属性)它length
仍然为零,因此 jQuery(正确地)什么都不做。
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 no length
property (not by default, anyway) jQuery will know that you really want to iterate over its propertiesinstead of numeric indices as in an Array.
正如其他人所指出的,您想要做的是使用例如创建一个对象var cells = {};
。由于非 Array 对象没有length
属性(无论如何默认情况下都没有),jQuery 会知道您确实想要迭代它的属性而不是像 Array 中的数字索引。
回答by Frédéric Hamidi
You seem to be thinking Javascript's arrays are associative, which is not the case. You're probably looking for objects (or hashes) instead:
您似乎认为 Javascript 的数组是关联的,但事实并非如此。您可能正在寻找对象(或哈希):
var cells = {}; // Not [].
$("td").each(function() {
var td = $(this);
cells[td.attr("id")] = td;
});
$.each(cells, function() {
console.log(this); // This should work as expected.
});
回答by Lee
use $.each(cells, function(i) { ... })
instead of $(cells).each(function...)
使用$.each(cells, function(i) { ... })
代替$(cells).each(function...)
The $.each()
function is different from the $(selector).each
function.
该$.each()
功能是由不同的$(selector).each
功能。