JavaScript Basics-higher-order functions (map and reduce)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.