简体   繁体   English

使用jQuery .each迭代关联数组

[英]Iterating an associative array with 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: 然后我在其中放入一些值(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 当我通过替换将关联数组更改为正常数字索引1

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? Doeas意味着jQuery的.each方法不接受关联数组或者我做错了什么?

JavaScript does not have associative arrays. JavaScript没有关联数组。 It has Arrays and it has Objects, and arrays happen to be objects. 它有Arrays,它有Objects,而数组恰好是对象。 When you do this: 当你这样做:

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 property called foo to the object a , not adding an element to the array a . 也就是说,您实际上是在对象 a添加了一个名为foo属性而不是数组中添加元素 a

From the documentation for jQuery.each() : jQuery.each()的文档:

Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1 . 具有length属性的数组和类似数组的对象(例如函数的arguments对象)由数字索引迭代,从0length-1 Other objects are iterated via their named properties. 其他对象通过其命名属性进行迭代。

Since you created an Array ( [] ) jQuery looks at its length property, and since you have not added any elements to the array (only properties on 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 eg var cells = {}; 正如其他人所指出的,你要做的是使用例如var cells = {};创建一个Object 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 properties instead of numeric indices as in an Array. 由于非Array对象没有length属性(不管是默认情况下),jQuery会知道你真的想要迭代它的属性而不是数组中的数字索引。

You seem to be thinking Javascript's arrays are associative, which is not the case. 您似乎认为Javascript的数组是关联的,但事实并非如此。 You're probably looking for objects (or hashes) instead: 您可能正在寻找对象(或哈希):

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.
});

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函数不同。

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

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