Higher-order function definition

Higher-order functions are functions that operate on other functions, either as arguments or by returning them. Simply put, a higher-order function is a function that takes a function as an argument or returns a function as output.

Higher-order functions common in JavaScript

The map:

The map() method creates a new array by calling the callback function provided as an argument to each element in the input array. The map() method takes each returned value from the callback function and uses it to create a new array.

Array. map(function(currentValue,index,arr), thisValue)

parameter describe
function(currentValue,index,arr) Must be. function
currentValue Must be. Current element.
index Optional. The index value of the current element.
arr Optional. The array object to which the current element belongs.
thisValue Optional. Object is used when the callback is executed, passed to the function, and used as the value of “this”. If thisValue is omitted, the value of “this” is” undefined”.

Example: multiply each item in the array [1,2,3,4] by 2 and print the array;

Not higher order functions

Var arr = [1, 2, 3, 4]; var arr1 = [];for(var i = 0; i < arr.length; i++) { arr1.push(arr[i] * 2); } console.log(arr1); / /,4,6,8 [2]Copy the code

Higher-order function map

Var arr = [1, 2, 3, 4]; var arr1 = arr.map(item => item * 2); cosnole.log(arr1); / /,4,6,8 [2]Copy the code

The results of the two runs are the same, but higher-order functions are relatively cleaner.

Reduce:

Syntax: array.reduce(function(Total, currentValue, currentIndex, ARR), initialValue)

parameter describe
function(currentValue,index,arr) Must be. function
total A necessity. The initial value, or the return value at the end of the calculation.
currentValue Must be. Current element.
index Optional. The index value of the current element.
arr Optional. The array object to which the current element belongs.
thisValue Optional. The initial value passed to the function, the initial value of total.

Array reduce() applies a function to Array [x1, x2, x3…]. The function must take two arguments, reduce(), and continue the cumulative calculation with the next element of the sequence, resulting in:

Fly higher order functions

var arr = [1, 3, 5, 7, 9];
var str = 0;
for(i = 0; i < arr.length; i++) { str += arr[i] } console.log(str); / / 25Copy the code

The higher-order function reduce

var arr = [1, 3, 5, 7, 9];
var str = arr.reduce(function (x, y) {
    returnx + y; }); console.. log(str); / / 25Copy the code

filter:

Filter is also a common operation to filter out some elements of an Array and return the rest. Like map(), filter() of Array receives a function. Unlike map(), filter() applies the passed function to each element in turn, and then decides whether to keep or discard the element depending on whether the value returned is true or false.

Filter Higher-order functions

Var arr = [1, 2, 4, 5, 6, 9, 10, 15]; var arr = [1, 2, 4, 5, 6, 9, 10, 15] var r = arr.filter(function (x) {
    returnx % 2 ! = = 0; }); r; Var arr = [1, 5, 9, 15];'A'.' '.'B', null, undefined, 'C'.' '];
var r = arr.filter(function (s) {
    returns && s.trim(); // Note: Versions below IE9 do not have the trim() method}); arr; / / /'A'.'B'.'C']
Copy the code

Of course, not only these three, there are other higher-order functions such as sort, you can also write higher-order functions, details can visit W3Cschool related tutorial, link address.