使用 jQuery .each 迭代关联数组

发布于 2024-12-08 13:46:56 字数 694 浏览 1 评论 0原文

可能对这个问题最有影响的因素是我现在非常困。

我有一个数组,我启动它:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

欢烬 2024-12-15 13:46:56

JavaScript 没有关联数组。它有数组,也有对象,而数组恰好是对象。当您执行此操作时:

var a = [];
a['foo'] = 'bar';

..您实际上正在执行与此等效的操作:

var a = [];
a.foo = 'bar';
// ^--- property of object 'a'

也就是说,您实际上正在将名为 foo属性添加到对象 a元素添加到数组 a

jQuery.each() 的文档中:

具有 length 属性的数组和类似数组的对象(例如函数的 arguments 对象)通过数字索引进行迭代,从 0长度-1。其他对象通过其命名属性进行迭代。

由于您创建了一个 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:

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.

From the documentation for 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. 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.

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 properties instead of numeric indices as in an Array.

寂寞陪衬 2024-12-15 13:46:56

您似乎认为 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.
});

You seem to be thinking Javascript's arrays are associative, which is not the case. 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.
});
梦境 2024-12-15 13:46:56

使用 $.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文