美文网首页
jquery 数组遍历、对象遍历

jquery 数组遍历、对象遍历

作者: 流浪嘚蒲公英 | 来源:发表于2017-06-21 15:55 被阅读0次

数组遍历:

var arr = [1, "hh", 5];

$(arr).each(function(index, item) {

// index: 下标; item: 元素值

// $(this): 获取的是一个数组; item: 获取的是一个对象

console.log(index, item, $(this));

});

对象遍历:

var obj = {name: "hh", sex: "male", age: "18"};

方法一:

$.each(obj, function(i, con) {

// i: 属性; con || obj[i]: 属性值

// $(this): 获取的值 == con.split("");

console.log(i, con, obj[i], $(this));

});

方法二:

for (j in obj) {

// i: 属性; obj[j]: 属性值

console.log(j, obj[j]);

}

相关文章

网友评论

      本文标题:jquery 数组遍历、对象遍历

      本文链接:https://www.haomeiwen.com/subject/fqhtcxtx.html