Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of the map () method in es6

Shulou Source: shulou.com Published: 2022-06-03 08:37:33 11月15日 Update

This article will explain in detail what is the use of the map () method in es6. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

In es6, the map () method is used to process each element in the array by calling the specified callback function and return the array containing the results; the syntax "array.map (function callbackfn (value, index, array), thisArg);".

The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.

The JavaScript map () method calls the specified callback function on each element of the array and returns the array containing the results.

Array.map (function callbackfn (value, index, array), thisArg)

Function callbackfn (value, index, array): a callback function that accepts up to three parameters:

Value: the value of the array element.

Index: a numeric index of array elements.

Array: the array object that contains this element.

The map () method returns a new array where each element is the callback function return value of the associated original array element. For each element in the array, the map () method calls the callbackfn function once (in ascending indexing order) and does not call the callback function for the missing elements in the array.

Except for array objects, the map () method can be used by any object that has a length property and has an indexed property name, such as an Arguments parameter object.

Let's take a look at the code example:

Example 1: double the values of all elements (that is, multiply by 2)

Var a = [30 value 40 50]; function f (value) {return value*2;} var a1=a.map (f); console.log (A1)

Output result:

Example 2: using JavaScript built-in methods as callback functions

Var a = [9,16]; var A1 = a.map (Math.sqrt); console.log (A1); / / 3mem4

Output result:

This is the end of the article on "what is the use of the map () method in es6". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Array element method function object result example article index parameter attribute more output good original practical three code content ascending order Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Huawei Redmi OPPO Reno NVidia