jquery 数组遍历、对象遍历

2017-06-21  本文已影响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]);

}

上一篇 下一篇

猜你喜欢

热点阅读