Array----Map

Source: Internet
Author: User

Map Method (Array) (JavaScript)

Each element of the array invokes the defined callback function and returns an array containing the results.

Grammar
Array1.map (callbackfn[, Thisarg])
Parameters

Parameters

Defined

array1

> must be selected.   

callbackfn

> must be selected.    A function that can accept up to three parameters.    map   method will call   callbackfn   function once.   

Thisarg

Optional.  The This keyword in the callbackfn function can refer to the object.  if thisargis omitted, undefined is used as the This value.

return value

A new array in which each element is the return value of the callback function for the associated original array element.

Exception

If the callbackfn parameter is not a function object, a TypeError exception is thrown.

Exception Condition
Notes

For each element in the array, themap method calls the callbackfn function once (in ascending index order).  The callback function will not be called for missing elements in the array.

In addition to array objects, themap method can be used by any object that has a length property and has a property name indexed by number.

callback function Syntax

The syntax for the callback function is as follows:

function Callbackfn (value, index, array1)

You can declare a callback function with up to three parameters.

The following table lists the callback function parameters.

Callback Parameters

Defined

Value

The value of the array element.

Index

The numeric index of the array element.

Array1

The array object that contains the element.

modifying an Array object

An array object can be modified by a callback function.

The following table describes the results obtained from modifying an array object after the map method is started.

Conditions after the map method is started

Is the element passed to the callback function?

Adds an element outside the original length of the array.

Whether.

Add elements to fill the missing elements in the array.

Yes, if the index has not been passed to the callback function.

The element has changed.

Yes, if the element has not been passed to the callback function.

Removes an element from the array.

No, unless the element has been passed to the callback function.

Example

The following example shows the use of the map method.

Javascript
Define the callback function.function areaofcircle (RADIUS) {    var area = Math.PI * (RADIUS * radius);    Return area.tofixed (0);} Create an array.var radii = [Ten, 30];//Get the areas from the radii.var areas = Radii.map (areaofcircle);d OCUMENT.W Rite (areas);//output://314,1257,2827
Example

The following example illustrates the use of the thisarg parameter, which specifies the object that the This keyword can refer to.

Javascript
Define An object this contains a divisor property and//a remainder Function.var obj = {    divisor:10,    remainder : function (value) {        return value% This.divisor;    }}  Create an Array.var numbers = [6, +, 30];//Get the remainders.//the obj argument specifies the this value in the Callback Function.var result = Numbers.map (Obj.remainder, obj);d ocument.write (result);//output://6,2,5,0
Example

In the following example, the built-in JavaScript method is used as a callback function.

Javascript
Apply math.sqrt (value) to all element in an array.var numbers = [9, 16];var result = Numbers.map (math.sqrt);d OCUMENT.W Rite (Result);//output:3,4
Example

The map method can be applied to a string.  This is illustrated in the following example.

Javascript
Define the callback function.function threechars (value, index, str) {    //Create A string that contains the previous , current,    //And next character.    Return str.substring (index-1, index + 2);} Create a String.var word = "Thursday";//Apply The map method to the string.//each of the array element in the result contain s a string that//have the previous, current, and next character.//the commented out statement shows an alternative syntax . var result = [].map.call (Word, threechars);//var result = Array.prototype.map.call (Word, Threechars);d Ocument.write ( result);//output://Th,thu,hur,urs,rsd,sda,day,ay

Array----Map

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.