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
版权声明:本文为博主原创文章,未经博主允许不得转载。
文章来自:http://blog.csdn.net/liuao107329/article/details/46697785