js 的each()方法遍历对象和数组
<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>
输出结果:
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