js常见遍历操作小结
人气:0本文实例讲述了js常见遍历操作。分享给大家供大家参考,具体如下:
for循环
var a = new Array("first", "second", "third") for(var i = 0;i < a.length; i++) { document.write(a[i]+","); }
for in
var arr = new Array("first", "second", "third") for(var item in arr) { document.write(arr[item]+","); }
for each
<button onclick="numbers.forEach(myFunction)">点我</button> <p id="demo"></p> <script> demoP = document.getElementById("demo"); var numbers = [4, 9, 16, 25]; function myFunction(item, index) { demoP.innerHTML = demoP.innerHTML + "index[" + index + "]: " + item + "<br>"; } </script>
var arr = [1,2,3,4]; arr.forEach(function(value,index,array){ array[index] == value; //结果为true sum+=value; }); console.log(sum); //结果为 10
each
$.each([],function(index,value,array){ //code something })
map
[].map(function(value,index,array){ //code })
PS:这里再为大家推荐一款JS数组遍历方式分析对比工具供大家参考:
在线JS常见遍历方式性能分析比较工具:http://tools.softyun.net/aideddesign/js_bianli
希望本文所述对大家JavaScript程序设计有所帮助。
您可能感兴趣的文章:
加载全部内容