如何在jQuery中循环遍历数组?

286
我正在尝试循环遍历一个数组。我有以下代码:
 var currnt_image_list= '21,32,234,223';
 var substr = currnt_image_list.split(','); // array here

我尝试获取数组中的所有数据,请问有谁能指点我正确的方向吗?

12个回答

0

使用具有副作用的替代迭代数组/字符串的方法

var str = '21,32,234,223';
var substr = str.split(',');

substr.reduce((a,x)=> console.log('reduce',x), 0)        // return undefined

substr.every(x=> { console.log('every',x); return true}) // return true

substr.some(x=> { console.log('some',x); return false})  // return false

substr.map(x=> console.log('map',x));                    // return array
 
str.replace(/(\d+)/g, x=> console.log('replace',x))      // return string


0
  for(var key in substr)
{
     // do something with substr[key];

} 

3
小心使用 for-in 迭代数组是为什么不好的主意? - General Grievance
请在您的答案中添加一些解释,以便其他人可以从中学习 - 特别是这是一个非常古老的问题,已经有了一些答案。 - Nico Haase

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接