javascript - JQuery: cannot loop dynamic array through $.each - Stack Overflow

Why cannot loop an dynamic array through $.each()?var array = [];array['one'] = 'two�

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
Add a ment  | 

1 Answer 1

Reset to default 14

For 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744094693a4557726.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信