您的位置:首页 > Web前端 > JavaScript

js 的each()方法遍历对象和数组

2015-06-30 16:51 701 查看
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<script src="../lib/jquery-1.8.3.min.js" ></script>
<script type="text/javascript" charset="utf-8">

var arr = ["a", "b", "c", "d", "e"];
var obj = { a: 'one', b: 'two', c: 'three', d: 'four', e: 'five' };

$.each(obj,function(key,value){
console.log("Obj :" + key + '-' + value);
})

$.each(arr,function(key,value){
console.log("arr :" + key + '-' + value);
})
</script>
</body>
</html>


输出结果:

Obj :a-one

Obj :b-two

Obj :c-three

Obj :d-four

Obj :e-five

arr :0-a

arr :1-b

arr :2-c

arr :3-d

arr :4-e
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: