jq中的each函数的用法

1.遍历对象

1.1 无参数

$.each(obj, function(key, value){
    this    // this指当前属性的值
    key    // obj当前属性的名称
    value // 当前属性的值
});

1.2 有参数

$.each(obj, function(){
    this    // 这里的this 指向的是每次遍历中的obj当前的值
    p1  p2    // 访问附加参数
},['参数1', '参数2']);

2. 遍历数组

2.1 无参数

$.each(arr, function(index, value){
    this      // this指向当前元素
    index    // i表示arr当前的下标
    value    // 表示的是当前的元素
});

2.2 有参数

$.each(arr, function(p1, p2){
    this    // 遍历到的当前的元素
    p1  p2    // 访问时候的附加的参数
}, ['参数1','参数2']);

 

示例

1.遍历数组

 var arr = [
   [1,4,2],
   [3,6,9],
   [5,6,1]  
];
$.each(arr,function(index, item){
    console.log(item[0]);    //  1, 3, 5
});

2.遍历对象

var data = {
   one:1,
   two:2,
   three:3,
   four:4 
};

$.each(data, function( key, val ){
    console.log( data[key] );   // 1,2,3,4
});

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-05
  • 2021-12-22
  • 2022-12-23
  • 2021-12-03
  • 2021-11-01
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
  • 2021-12-10
  • 2022-12-23
相关资源
相似解决方案