我试图通过一个数组循环。我有以下代码:

 var currnt_image_list= '21,32,234,223';
 var substr = currnt_image_list.split(','); // array here

我试图把所有的数据从数组。有谁能指引我正确的道路吗?


当前回答

带有箭头函数和插值的ES6语法:

var data=["a","b","c"];
$(data).each((index, element) => {
        console.log(`current index : ${index} element : ${element}`)
    });

其他回答

使用jQuery的each()函数。

这里有一个例子:

$.each(currnt_image_list.split(','), function(index, value) { 
  alert(index + ': ' + value); 
});

使用jQuery each()。还有其他方法,但每一种都是为这个目的而设计的。

$.each(substr, function(index, value) { 
  alert(value); 
});

不要在最后一个数字后面加逗号。

你可以使用for()循环:

var things = currnt_image_list.split(','); 
for(var i = 0; i < things.length; i++) {
    //Do things with things[i]
}
  for(var key in substr)
{
     // do something with substr[key];

} 

带有箭头函数和插值的ES6语法:

var data=["a","b","c"];
$(data).each((index, element) => {
        console.log(`current index : ${index} element : ${element}`)
    });