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