admin 管理员组文章数量: 1086019
Why cannot loop an dynamic array through $.each()
?
var array = [];
array['one'] = 'two';
$.each(array, function( key, value )
{
//not get in loop
alert(value);
});
Why cannot loop an dynamic array through $.each()
?
var array = [];
array['one'] = 'two';
$.each(array, function( key, value )
{
//not get in loop
alert(value);
});
Share
Improve this question
asked Jan 5, 2017 at 20:41
user3014233user3014233
0
1 Answer
Reset to default 14For an array, $.each()
only loops through the numbered indexes. If you want to loop through named properties, you have to use an object.
var obj = {};
obj['one'] = 'two';
$.each(obj, function( key, value )
{
console.log(key, value);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
This is explained in the documentation:
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.
本文标签: javascriptJQuery cannot loop dynamic array through eachStack Overflow
版权声明:本文标题:javascript - JQuery: cannot loop dynamic array through $.each - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744094694a2532681.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论