简体   繁体   中英

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();

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.

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.

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?

UPDATE 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

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?

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() .

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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