The embodiment of functional programming in JavaScript--map and reduce

Source: Internet
Author: User
Tags new set

Recently learning JavaScript, in the middle see map and reduce method, feel very interesting, learn to write this blog.

Both of these functions embody the idea of functional programming to some extent, and the function is passed as an argument to another function.

The caller of the map () method is typically an array, and the parameter is a function called callback, which is a new set of return values that each element in the original array performs for the given callback function.

That is, when you use the map () method, each element in the array is passed into the given function as an argument, and if the function has a return value, the return value of each execution function will be returned as a new array.

function pow(x) {    return x * x;}var arr = [123456789// [1, 4, 9, 16, 25, 36, 49, 64, 81]

Above we implemented the map () method to use each element in the ARR array as a parameter to the POW () method, and the resulting return value is the collection of the return value of each execution of the POW () function, which is an array.

Of course callback function can also have no return value, at this time, the view callback function returns undefined.

var  arr = [0  , 1 , 2 , 3 , Span class= "Hljs-number" >4 , 5 ]; var  arr1 = Arr.map (function  (item) {console . Log (item)}); // print 0  1  2  3  4  5 //arr1 for [undefined , undefined , undefined , undefined , undefined , undefined ] 

Again, the reduce () method, like the map () method, is an array, but the callback function here is somewhat different, requiring that two parameters must be received.

The reduce () method executes the callback function on the first and second elements in a left-to-right order, continues the resulting result as a parameter, and executes the callback function on the next element until the last element in the array, and constructs a final return value.

var arr = [13579];arr.reduce(function (x, y) {    return// 25

This is an example of using the reduce () method to sum an array.

It is important to note that in addition to the callback function as a parameter, the reduce () method can have a initialvalue as a parameter, and if InitialValue is specified, InitialValue is used as the first parameter of the callback. The first element in the array as the second argument.

The embodiment of functional programming in JavaScript--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.