首页 > 代码库 > non1 遍历
non1 遍历
1.
var arr = [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘]for (var i = 0, l = arr.length; i < l; i++) { console.log(i, arr[i])}
输出:
2.
var arr = [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘]for (var l = arr.length; l--;) { console.log(l, arr[l])}
输出:
var arr = [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘]for (var l = arr.length; l--;) { arr.splice(l, 1)}console.log(arr) // []
3.
var arr = [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘]var l = arr.lengthwhile(l--) { console.log(l, arr[l])}
输出:
4. ES5
var arr = [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘]arr.forEach(function(value, index, array) { console.log(index, value, array)})
输出:
5. Underscore.js
<script src="http://www.mamicode.com/underscore.js"></script><script> var list = [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘] _.each(list, function(element, index, list) { console.log(element, index, list, this) }, list) _.each({one: 1, two: 2, three: 3}, function(value, key, list) { console.log(value, key, list) })</script>
输出:
6. jQuery
<script src="http://www.mamicode.com/jquery-1.10.2.js"></script><script> var list = [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘] $.each(list, function(index, value) { console.log(index, value) }) $.each({one: 1, two: 2, three: 3}, function(key, value) { console.log(key, value) })</script>
输出:
non1 遍历
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。