Use of map () method to operate arrays in JavaScript

Source: Internet
Author: User
This article mainly introduces how to use the map () method for operating arrays in JavaScript. It is the basic knowledge in JS beginners. If you need it, refer to the JavaScript array map () method to create a new array using the result of calling the function provided on each element in this array.
Syntax

array.map(callback[, thisObject]);

The following is the detailed information about the parameters:

Callback: Generate Elements of the new array from the current element function.

ThisObject: the object used for executing the callback

Return Value:

Return to create an array
Compatibility:

This method is a JavaScript extension to the ECMA-262 standard; so it may not exist in other implementations of the standard. To make it work, you need to add the following script code at the top:

if (!Array.prototype.map){ Array.prototype.map = function(fun /*, thisp*/) {  var len = this.length;  if (typeof fun != "function")   throw new TypeError();   var res = new Array(len);  var thisp = arguments[1];  for (var i = 0; i < len; i++)  {   if (i in this)    res[i] = fun.call(thisp, this[i], i, this);  }   return res; };}

Example:

JavaScript Array map Method

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.