码迷,mamicode.com
首页 > 其他好文 > 详细

non1 遍历

时间:2016-08-26 12:00:59      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

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.length
while(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="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="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 遍历

标签:

原文地址:http://www.cnblogs.com/jzm17173/p/5809580.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!