开发者社区> 问答> 正文

如何在jQuery中遍历数组?

我试图遍历一个数组。我有以下代码:

var currnt_image_list= '21,32,234,223'; var substr = currnt_image_list.split(','); // array here 我正在尝试从数组中获取所有数据。有人可以带领我走上正确的道路吗?

展开
收起
保持可爱mmm 2020-01-16 15:52:41 430 0
1 条回答
写回答
取消 提交回答
  • jQuery.each() jQuery.each()

    jQuery.each(array, callback) 数组迭代

    jQuery.each(array, function(Integer index, Object value){}); 对象迭代

    jQuery.each(object, function(string propertyName, object propertyValue){}); 例如:

    var substr = [1, 2, 3, 4]; $.each(substr , function(index, val) { console.log(index, val) });

    var myObj = { firstName: "skyfoot"}; $.each(myObj, function(propName, propVal) { console.log(propName, propVal); });

    运行代码段展开摘要 数组的javascript循环 for循环

    for (initialExpression; condition; incrementExpression) statement 例

    var substr = [1, 2, 3, 4];

    //loop from 0 index to max index for(var i = 0; i < substr.length; i++) { console.log("loop", substr[i]) }

    //reverse loop for(var i = substr.length-1; i >= 0; i--) { console.log("reverse", substr[i]) }

    //step loop for(var i = 0; i < substr.length; i+=2) { console.log("step", substr[i]) } 运行代码段展开摘要 对于

    //dont really wnt to use this on arrays, use it on objects for(var i in substr) { console.log(substr[i]) //note i returns index } 的

    for(var i of subs) { //can use break; console.log(i); //note i returns value } 每次

    substr.forEach(function(v, i, a){ //cannot use break; console.log(v, i, a); }) 问题来源于stack overflow

    2020-01-16 15:53:08
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关课程

更多

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载