Posted in Javascript onJune 06, 2019
本文实例讲述了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 })
感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools.3water.com/code/HtmlJsRun测试上述代码运行效果。
js常见遍历操作小结
- Author -
embaobao声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@