jQuery.each() 如何处理关联数组(对象)?
我有一个关联数组,里面有两个对象。通过 $(myassoc).each() 运行此回调,回调仅运行一次。回调参数(索引和对象)也分别返回 0 和整个关联数组。
人们期望 jQuery.each() 为数组中的每个元素运行,返回正确的键作为索引,返回正确的元素作为对象。
为什么这种情况没有发生,jQuery 可以做我想要的事情吗?
I have an associative array with two object inside. Running this through $(myassoc).each()
, the callback runs only once. Also the callback parameters (index and object) returns 0 and the entire associative array, respectively.
One would expect jQuery.each()
to run for each element in the array, returning the correct keys as index and the correct element as the object.
Why isn't that happening, and can jQuery do what I'm after?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您正在寻找 jQuery.each() 而不是 .each()
试试这个:
I think you're looking for jQuery.each() instead of .each()
try this:
试试这个:
try this:
糟糕的是。
不要
$(associative_array).each(function () {...})
—— 那是 废话不要
$.each(associative_array, function() {...});
-- 有一个晦涩的 bug(1)要查看该错误,请尝试this 在 javascript 控制台中:
第一个示例输出三行键值对,正如它应该的那样。第二个没有输出!
这个故事的寓意是,不要在对象上使用 jQuery.each()。 (JavaScript 中的对象本质上是与关联数组相同。)事情可能永远工作正常,但你运行有一天,一个对象碰巧有一个名为
length
的成员,并且它的值恰好是0
,然后你会突然出现一个很难修复的错误。解释。 (我会让你猜,通过这个答案的沉重分量,这是否发生在我身上。)正如 错误报告:
我建议更进一步,永远不要依赖 jQuery.each 来实现关联数组。
(1) 这个“bug”可能永远不会被修复,因为 $.each() 历史上使用 Duck在数组上键入:“具有长度属性的数组和类似数组的对象(例如函数的参数对象)通过数字索引进行迭代。”
这是我使用的[感谢 Dominik] 来循环遍历对象的属性名称和值,或者换句话说,关联数组的键和值:
的直接替代品
looper() 是 $.each() 就像 $.each() 一样:
this
是false
(不仅仅是 falsy ) 终止循环。使用:
用 $.each() 尝试一下,你会得到一个空结果。因为它将这个特定对象解释为长度为零的类似数组的对象。
Badly.
Don't
$(associative_array).each(function () {...})
-- that's nonsenseDon't
$.each(associative_array, function() {...});
-- that has an obscure bug(1)To see the bug, try this in a javascript console:
The first example outputs three lines of key-value pairs, as it should. The second outputs nothing!
The moral of the story, don't use jQuery.each() on objects. (Objects in JavaScript are essentially the same thing as associative arrays.) Things may work fine forever, but you run the risk that someday an object happens to have a member named
length
and its value happens to be exactly0
and then you have a bug out of nowhere that can be very difficult to explain. (I'll let you guess, by the ponderous heft of this answer, whether that ever happened to me.)As mentioned in the bug report:
I suggest going further, that jQuery.each should not be relied upon for associative arrays, ever.
(1) This "bug" may never be fixed, since $.each() historically uses Duck Typing on arrays: "Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index."
Here's what I use[thanks Dominik] to loop through property names and values of objects, or put another way, the keys and values of an associative array:
looper() is then a drop-in replacement for $.each()
Just like $.each():
this
is each valuefalse
(not just falsy) terminates the loopUse:
Try that with $.each() and you'll get an empty result. Because it interprets this particular object as an array-like object of zero length.
问题在于
$.each()
函数在内部检索并使用传递集合的length
属性。但在没有整数索引的关联数组中,长度
似乎总是0
。对于$.each()
现在似乎没有什么可遍历的。解决方案很简单,就是使用一个对象来代替。
The problem is that the
$.each()
function internally retrieves and uses thelength
property of the passed collection. But in an associative array that has no integer indices thelength
always seems to be0
. For$.each()
now there seems to be nothing to walk through.The solutions is simply to use an object instead.