1 //Higher order functions: a function can accept another function as a parameter, which is called a higher order function. 2 /*1*/3 functionF (x,y,f) {4 returnF (x) +f (y);5 }6 varSumabs=f ( -6,4, math.abs);7Console.log (Sumabs);//Ten8 9 Ten One //map and reduce A /*Map*/ - /*2*/ - functionPow (x) { the returnx*x; - } - vararr=[1,2,3,4,5]; - varX2=Arr.map (POW); + vartos=Arr.map (String); -Console.log (x2);//[1, 4, 9, +,] +Console.log (ToS);//["1", "2", "3", "4", "5"] A at - /*Reduce*/ - /*3*/ - vararr=[1,3,5,7,9]; - functionsum (x, y) { - returnx+y; in } - functionPro (x, y) { to returnx*y; + } - functionIntt (x, y) { the returnx*10+y; * } $ vars=arr.reduce (sum);Panax Notoginseng varp=arr.reduce (pro); - varI=arr.reduce (INTT); theConsole.log (s);// - +Console.log (P);//945 AConsole.log (i);//13579 the + /*or*/ -Console.log (Arr.reduce (function(x, y) { $ returnx+y; $}));// - -Console.log (Arr.reduce (function(x, y) { - returnx*y; the}));//945 -Console.log (Arr.reduce (function(x, y) {Wuyi returnx*10+y; the}));//13579 - Wu - /*string to int*/ About /*4*/ $ varStr= "5678"; - varArr=str.split (' '); -Console.log (arr);//["5", "6", "7", "8"] -Arr=arr.map (function(x) { A returnx-0; + }) theConsole.log (arr);//[5, 6, 7, 8] -Console.log (Arr.reduce (function(x, y) { $ returnX*10+y;//5678 the })) the the /*or*/ theConsole.log (Str.split ("). Map (function(x) { - returnx-0; in}). Reduce (function(x, y) { the returnx*10+y; the}));//5678 About the the /*Please change the user's non-standard English name into the first letter, the other lowercase canonical name. Input: [' Adam ', ' Lisa ', ' Bart '], output: [' Adam ', ' Lisa ', ' Bart ']. */ the + /*Test*/ - varStr= "ABS"; theConsole.log (Str[0]);//aBayi /*5*/ the vararr=[' Adam ', ' LISA ', ' BarT ']; theConsole.log (Arr.map (function(x) { - returnX[0].touppercase () +x.substring (1). toLowerCase () -}));//["Adam", "Lisa", "Bart"]
JavaScript Basics-higher-order functions (map and reduce)