map,reduce,filter和sort

        'use strict';
        //map()方法定义在array上,可接收一个函数
        function pow(x) {
            return x * x;
        }
        var arry = [1, 2, 3, 4, 5];
        //console.log(arry.map(pow));//[1,4,9,16,25] 
       'use strict';
        var arry = [1, 2, 3, 4, 5];
        function sum(x, y) {
            return x + y;
        }
        //reduce的函数必须有2个参数
        console.log(arry.reduce(sum));//15
        var result = arry.reduce(function (x, y) {
            return x + y;
        });
        console.log("result=" + result);//result=15

 

       'use strict';
        var arry = [1, 2, 3, 4, 5];
        //filter用于过滤某些元素
        var newArray = arry.filter(function (element, index, self) {
            return element % 2 == 0;
        });
        console.log("newArray=" + newArray);//newArray=2,4 

 

上一篇:Python实现十进制和二进制之间相互转换


下一篇:手撸冒泡排序