Map method of arrays in JavaScript

Source: Internet
Author: User

Map method Prototype:Array1.map (callbackfn[, Thisarg])

Parameters:

Array1, must be selected. an Array object. This function is typically used for array objects

CALLBACKFN, must be selected. A function that can accept up to three parameters. for each element in the array, themap method calls the Callbackfn function one time.

Thisarg, optional. The This keyword in the CALLBACKFN function can refer to the object . if Thisarg is 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 .

Description

for each element in the array, the map 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.

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.

Value of the array element.

Index, the number of the array element.

An array1 that contains the array object for the element.

Instance:

//Define the callback function.functionareaofcircle (RADIUS) {varArea = Math.PI * (RADIUS *radius); returnArea.tofixed (0); }  //Create an array.varradii = [10, 20, 30]; //Get The areas from the radii.varAreas =Radii.map (areaofcircle);  document.write (areas); //Output://314,1257,2827

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

//Define An object, contains a divisor property and//a remainder function.varobj ={divisor:10, remainder:function(value) {returnValue% This. Divisor; } }  //Create an array.varnumbers = [6, 12, 25, 30]; //Get the remainders.//The obj argument specifies the this value in the callback function.varresult =Numbers.map (Obj.remainder, obj); document.write (result); //Output://6,2,5,0

Note: This function is not supported in IE8 and IE8 browsers below.

Original: Https://msdn.microsoft.com/zh-cn/express/ff679976%28v=vs.90%29

Map method of arrays in JavaScript

Related Article

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.