1.forEach():每个元素都调用指定函数,可传三个参数:数组元素丶元素索引丶数组本身丶 var a = [1, 2, 3, 4, 5, 6, 7, 8]; a.forEach(function(v,i,a){a[i]=v+1;}); console.log(a); //[2, 3, 4, 5, 6, 7, 8, 9] 2.map():每个元素传递给指定函数,并返回该数组 var b = [1, 2, 3, 4, 5, 6, 7, 8]; console.log( b.map(function(x){return x*x}) ); //[1, 4, 9, 16, 25, 36, 49, 64] 3.filter():用于筛选数组里的元素 并返回满足的子集 var c = [1, 2, 3, 4, 5, 6, 7, 8]; console.log( c.filter(function(x){return x>5}) ); //[6, 7, 8] console.log( c.filter(function(x,i){return i%2==0}) ); //这里返回索引为偶数的数组元素 4.every(),some():用于判断(全部/部分)数组里的元素 并返回bool var d = [1, 2, 3, 4, 5, 6, 7, 8]; console.log( d.every(function(x){return x>5}) ); //false console.log( d.some(function(x){return x>5}) ); //true 5.reduce():用指定函数将数组元素进行组合 生成并返回单个值 var e = [1, 2, 3, 4, 5, 6, 7, 8]; console.log( e.reduce(function(x,y){return x+y},0)); //36 这里的0为传递给函数的初始值 /* 这里内部运行方法为: 0+1=1;1+2=3;3+3=6;6+4=10;10+5=15;15+6=21;21+7=28;28+8=36; */ 使用函数处理数组: /*1.计算平均值和标准差 */ var sumFun = function(x,y){return x+y}; var squFun = function(x){return x}; var f = [1, 2, 3, 4, 5, 6, 7, 8]; var avergae = f.reduce(sumFun)/f.length; console.log("平均值为:"+avergae); //4.5 var devitation = f.map(function(x){return x-avergae}) console.log("平均值差数组:"+devitation); //-3.5,-2.5,-1.5,-0.5,0.5,1.5,2.5,3.5 高阶函数: 后面的还没看…待续…