简体   繁体   English

Jquery.each()不能遍历文字数组

[英]Literal Array can't be traverse by Jquery.each()

ok i do have this literal array declared as seen in a firebug screenshot below 好吧,我确实声明了此文字数组,如下面的萤火虫屏幕截图所示

在此处输入图片说明

now i tried to traverse it by using jquery.each(); 现在我试图通过使用jquery.each();遍历它jquery.each();

this my code 这是我的代码

$.each(window.feeditems,function(key,val){
   alert('pass OK');
   console.log('index:' + key + ', ' + 'value:' + val);
}); 
console.log('overview:' + window.feeditems);
console.log('length:' + window.feeditems.length);

ok so with the code above, the TODO inside the callback of $.each doesn't execute as expected the length is undefined when its printed it on the console. 好的,所以使用上面的代码, $.each回调中的TODO不能按预期执行,将其打印在控制台上时length undefined

well in a very strange case as i printed the window.feeditems in the firebug it displays my literal array completely like what displayed in the screenshot above. 在一个非常奇怪的情况下,当我在firebug中打印window.feeditems时,它完全显示了我的文字数组,就像上面屏幕截图中显示的那样。

now my question is how i can traverse this kind of array? 现在我的问题是如何遍历这种数组? i know jquery.each() relies on the length property of the array, and im also thinking of using for loop but how i can loop without knowing its size? 我知道jquery.each()依赖于数组的length属性,我还考虑使用for循环,但是我如何在不知道其大小的情况下循环?

UPDATE 1 更新1

i recheck again the length and transfer it somewhere, it returns length zero, but put it with the next line the content checker code, it still contains the contents of the array. 我再次检查长度并将其转移到某个地方,它返回长度为零,但将其与内容检查器代码的下一行放在一起,它仍然包含数组的内容。

UPDATE 2 更新2

Any possible workaround so I can traverse this array on the runtime without accessing the nodes manually in the code? 有什么可能的解决方法,这样我就可以在运行时遍历此数组,而无需手动访问代码中的节点? for example using a for loop? 例如使用for循环?

An array only accepts numeric indices. 数组仅接受数字索引。 You appear to be defining string keys instead, which will not count as part of the .length or show up in a .each() . 您似乎正在定义字符串键,这些键将不算作.length一部分,也不显示在.each()

Instead of starting your definition with [] , use {} to allow string keys. 不要使用[]开始定义,而使用{}允许使用字符串键。 However, keep in mind that you still won't be able to use .length . 但是请记住,您仍然无法使用.length

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

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