Javascript common higher-order functions details

Javascript common higher-order functions details

Higher Order function , called Higher Order function in English. A function that can receive another function as a parameter is called a higher-order function.

Example:

function add(x, y, f) {
 return f(x) + f(y);
}

//Verify with code:
add(-5, 6, Math.abs); // 11

1. Common higher-order functions

ES6 adds several new methods to arrays, among which map , reduce , and filter are all high-order functions. In addition, ordinary sort is also a high-order function. The three new methods are introduced below.

1.1、filter

filter is to filter the array, return the data that meets the conditions, form a new array and return it, and discard the data that does not meet the conditions.

Example 1: Take out the data less than 100 in the array and put it into a new array

let grad = [ 102, 188, 55, 66, 200, 800 ]
let arr2 = grad.filter( function(item){
 return item <= 100
})
console.log("arr2",arr2) // 55, 66

In the above example, the parameter passed to filter is a function. The passed function acts on each element in turn, and then decides whether to keep or discard the element based on whether the return value is true or false . Because only 55 and 66 meet the requirements, there are only these two elements in the new array.

1.2, map

Map means mapping.

The original array is mapped into a new array, and the return value is a new array, without changing the original array. The length of the new array will not change with the original array.

Example 2: Magnify each element of the data by 2 times.

let arr2 = [ 55, 66 ]
let arr3 = arr2.map( item => {
 return item*2
})
//Return result [110, 132]

In the above example, the parameter received by map is a function, which acts on each element in turn, magnifying the element by 2 times, and can also perform arbitrarily complex operations on it.

1.3、reduce

reduce is the process of summarizing arrays. Often an array goes in and a piece of data comes out. Often used for summing and calculating averages.

Example 3: Sum the results returned by the previous example.

let sum = arr3.reduce((tmp,item)=>{
 return tmp+item
})
//Return result 242

Now here comes the point. If we want to combine the above three examples together, how simple can we write it in the end?

Responsible for writing:

// Complex writing let grad = [102,188,55,66,200,800]
let arr2 = grad.filter(function(item){
 return item <= 100
})
let arr3 = arr2.map(item=>{
 return item*2
})
let sum = arr3.reduce((tmp,item)=>{
 return tmp+item
})

Simple writing:

//Simple writing let sum2 = grad
           .filter( item => {return item <= 100})
          .map(item=>{return item*2})
          .reduce((tmp,item)=>{return tmp+item})

This is the end of this article about the details of common higher-order functions Javascript . For more information about common higher-order functions Javascript , please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of JavaScript higher-order functions
  • In-depth study of JavaScript higher-order functions
  • Analysis of the usage of higher order functions in JavaScript functional programming
  • Analysis of the principles and usage examples of JS higher-order functions
  • A detailed explanation of the charm of higher-order functions in JavaScript
  • Share 5 JS high-order functions

<<:  Analysis of the process of building a LAN server based on http.server

>>:  Bugs encountered when using mybatis-generator with mysql8.0.3 in IDEA

Recommend

Learn Vue middleware pipeline in one article

Often when building a SPA, you will need to prote...

HTML+CSS to achieve text folding special effects example

This article mainly introduces the example of rea...

Solve the compatibility issue between MySQL 8.0 driver and Alibaba Druid version

This article mainly introduces the solution to th...

Learn MySQL index pushdown in five minutes

Table of contents Preface What is index pushdown?...

A question about border-radius value setting

Problem Record Today I was going to complete a sm...

MySQL 5.7.23 decompression version installation tutorial with pictures and text

It is too troublesome to find the installation tu...

Vue uses Baidu Maps to realize city positioning

This article shares the specific code of Vue usin...

How to implement remote access control in Centos 7.4

1. SSH remote management SSH is a secure channel ...

Linux implements automatic and scheduled backup of MySQL database every day

Overview Backup is the basis of disaster recovery...

Solution to the inconsistency between crontab execution time and system time

Preface In LINUX, periodic tasks are usually hand...

Is a design that complies with design specifications a good design?

In the past few years of my career, I have writte...

Usage of if judgment in HTML

In the process of Django web development, when wr...

CSS3 timeline animation

Achieve results html <h2>CSS3 Timeline</...

Several ways to encapsulate axios in Vue

Table of contents Basic Edition Step 1: Configure...