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

<!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

原文地址:https://www.cnblogs.com/gccbuaa/p/6858100.html