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

Analysis of MySQL's planned tasks and event scheduling examples

This article uses examples to describe MySQL'...

Install Memcached and PHP Memcached extension under CentOS

Regarding the high-performance distributed memory...

vue-electron problem solution when using serialport

The error is as follows: Uncaught TypeError: Cann...

8 Reasons Why You Should Use Xfce Desktop Environment for Linux

For several reasons (including curiosity), I star...

How to upgrade all Python libraries in Ubuntu 18.04 at once

What is pip pip is a Python package management to...

Building a selenium distributed environment based on docker

1. Download the image docker pull selenium/hub do...

HTML basic structure_Powernode Java Academy

Many times when learning web page development, th...

Exploring the practical value of the CSS property *-gradient

Let me first introduce an interesting property - ...

CSS easily implements fixed-ratio block-level containers

When designing H5 layout, you will usually encoun...

How to use the realip module in Nginx basic learning

Preface There are two types of nginx modules, off...

Discussion on image path issues in css (same package/different package)

In CSS files, sometimes you need to use background...

Detailed steps for installing and debugging MySQL database on CentOS7 [Example]

This example requires downloading and installing ...

About Docker security Docker-TLS encrypted communication issues

Table of contents 1. Security issues with Docker ...

Details of the order in which MySQL reads my.cnf

Table of contents The order in which MySQL reads ...

Introduction to the usage of common XHTML tags

There are many tags in XHTML, but only a few are ...