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
 
})

 

 

 

 

 

posted @ 2018-07-04 19:28  baobaoMaster  阅读(94)  评论(0编辑  收藏  举报