jquery遍历数组、对象

1,for循环:

var arr = new Array(13.5,3,4,5,6);
for(var i=0;i<arr.length;i++){
 arr[i] = arr[i]/2.0;
}

2,for,in循环:

var x
var mycars = new Array()
mycars[0] = "Saab"
mycars[1] = "Volvo"
mycars[2] = "BMW"
for (x in mycars)
{
  document.write(mycars[x] + "<br />")
}

2.1for,in 循环遍历对象

var data = {1: "食品标准物质", 2: "环境标准物质", 3: "职业卫生标准物质", 4: "仪器鉴定标准物质", 5: "药典及对照品", 6: "进口标准品", 7: "地质矿产标准物质", 8: "其他标准物质", 9: "化学试剂"};
for(var i in data){
 console.log(i);//索引
 console.log(data[i]);//值
}

  

3,each遍历一维数组:

var arr=new Array();
arr=["aaa","bbb","ccc"];
$.each(arr,function(index,value){
     alert(i+"..."+value);
});

3.1each遍历对象:

var data = {1: "食品标准物质", 2: "环境标准物质", 3: "职业卫生标准物质", 4: "仪器鉴定标准物质", 5: "药典及对照品", 6: "进口标准品", 7: "地质矿产标准物质", 8: "其他标准物质", 9: "化学试剂"};
$.each(data,function(i,n){
                console.log('索引' + i + ';对应值为:' + n +';');
            });

  

4,each遍历二维数组:

$(function () {
    $.each([["aaa", "bbb", "ccc"], ["ddd", "eee", "fff"], ["ggg", "hhh", "iii"]], function (index, item) {
         alert(index + "..." + item);
         //输出0...aaa,bbb,ccc  1...ddd,eee,fff  2...ggg,hhh,iii   这时的index为数组下标,item相当于取这二维数组中的每一个数组
         $.each(item, function (index, itemobj) {
              alert(index + "....." + itemobj);
         });
    });
     //输出0...aaa,bbb,ccc  0...aaa 1...bbb 2...cccc  1...ddd,eee,fff  0...ddd 1...eee 2...fff  2...ggg,hhh,iii 0...ggg 1...hhh 2...iii
 });

  

posted @ 2019-09-20 16:39  大尹  阅读(15002)  评论(0编辑  收藏  举报