1.for circulation
Normal traversal method can be optimized, able to save the array length, to avoid every time to get the array length, performance
for(var i=0;i<arr.length;i++){ console.log(arr[i]); }
2.for-in
Not only through the array can also traverse the object
for(key in obj)
The method may read the member attribute of the object itself, but also can traverse the object prototype properties
3.forEach
No return value, can return to exit the loop, you can not exit the loop with a break
1 var str = ""; 2 arr.forEach(function (ele,index,array) { 3 str+=ele; 4 }); 5 alert(str);
4.map
It returns a value, returns after traversing a new array does not change the original list, returns nothing added to the array, which is equivalent to each of the array becomes the value return
1 var arr2 = arr.map(function (ele,index,array) { 2 return ele+"你好"; 3 }) 4 5 console.log(arr2);
5.filter
Have a return value, return an array of true, does not change the original array, screening, it does not change the value of each array, selected to meet the conditions of the new array
1 var arr1 = arr.filter(function (ele,index,array) { 2 if(ele.length>2){ 3 return true; 4 } 5 return false; 6 }); 7 8 console.log(arr1);
6.every
Returns true or false, true if each array meet the conditions, otherwise it returns false, and the parameter is the callback function
var bool = arr.every(function (element,index,array) {
element = "aaa";
array[index] = "aaa";
if(element.length>2){
return false;
}
return true;
});
alert(bool);
7.find
Each element of the array of functions provided by execution, if a function in line with the requirements, the elements of this array is returned, the end of the cycle, otherwise undefined
8.reduce
Plus class. No return empty array value