var array = [23,48,66,2];
forEach:循环、遍历数组;没有返回值。与for循环类似array.forEach(function( value, index, array ) { console.log(value); return value * value; } );
map:映射的意思,映射返回一个新数组,有返回值;filterArr:返回一个新对象var mapArr = array.map(function(value, index, array ){ return value * value; });
filter:过滤、筛选的意思; 有返回值; filterArr:返回过滤后新数组var filterArr = array.filter(function(value, index, array ) { if ( value > 20 ) { return true; } return false; });
everyArr:每一个、每一项意思; 有返回值; every:返回boolean true or false;var everyArr = array.every(function(value, index, array){ if ( value > 1 ) { return true; } });
indexOf:第一个是检索的值,第二个是需要检索的位置从0开始console.log(array.indexOf(23,0));
lastIndexOf:第一个是检索的值,第二个是需要检索的位置从arr.length-1开始的console.log(array.lastIndexOf(2,10));
// 初始设置 previous = initialValue = 1, current = 2
// 第一次迭代 previous = (1 + 2) = 3, current = 3
// 第二次迭代 previous = (3 + 3) = 6, current = 4
// 第三次迭代 previous = (6 + 4) = 10, current = undefined (退出)var reduce = [1,2,3,4].reduce(function(prev, curr, index, array) { console.log(prev +" "+ curr); return prev + curr; });
// 初始设置 index = 3, previous = initialValue = 4, current = 3
// 第一次迭代 index = 2, previous = (4- 3) = 1, current = 2
// 第二次迭代 index = 1, previous = (1 - 2) = -1, current = 1
// 第三次迭代 index = 0, previous = (-1 + 1) = 0, current = undefined (退出)var reduceRight = [1,2,3,4].reduceRight(function(prev, curr, index, array) { if ( index == 0 ) { return prev + curr; } return prev - curr; });console.log(reduceRight);
ES5中Array新增加的API接口 forEach map filter some every indexOf lastIndexOf reduce reduceRight
原文:http://blog.csdn.net/itpinpai/article/details/50012173