Is map a method in es6?

WBOY
Release: 2022-04-25 18:37:52
Original
1789 people have browsed it

map is a method in es6; this method can call the specified callback function for each element of the array, and return the data containing the result. The return result is a new array, and the syntax is "array object.map (function callback function (value of array element, index of array element, array object));".

Is map a method in es6?

The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.

Is map a method in es6?

The map() method can call the specified callback function for each element of the array and return an array containing the results. The specific usage is as follows:

array.map(callbackfn[, thisArg]);
Copy after login

Parameter description:

  • array: required parameter, an array object.

  • callbackfn: required parameter, a function that can receive up to three parameters. For each element in the array, the map() method calls the callbackfn function once.

  • thisArg: Optional parameter, an object that can be referenced by the this keyword in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

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

In addition to array objects, the map() method can be used by any object with a length property and an indexed property name, such as an Arguments parameter object.

The syntax of the callback function is as follows:

function callbackfn (value, index, array);
Copy after login

Users can use up to three parameters to declare the callback function. The parameter description of the callback function is as follows:

  • #value: The value of the array element.

  • index: Numeric index of the array element.

  • array: Array object containing the element.

Example 1

The following example uses the map() method to map an array, square the value of each element in the array, and multiply it by the PI value , use the returned area value of the circle as the element value of the new array, and finally return the new array.

function f (radius) {
    var area = Math.PI * (radius * radius);
    return area.toFixed(0);
}
var a = [10,20,30];
var a1 = a.map(f);
console.log(a1);
Copy after login

Example 2

The following example uses the map() method to map an array, divide the value of each element in the array by a threshold, and then return the new array where Both the callback function and the threshold exist as properties of the object. This method demonstrates how to use the thisArg parameter in the map.

var obj = {
    val : 10,
    f : function (value) {
        return value % this.val;
    }
}
var a = [6,12,25,30];
var a1 = a.map(obj.f, obj);
console.log(a1);  //6,2,5,0
Copy after login

[Related recommendations: javascript video tutorial, web front-end

The above is the detailed content of Is map a method in es6?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
es6
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template