Grammar
var Mappedarray = Array.map (callback[, Thisobject]);
Parameter description
Callback: The callback function to execute for each array element.
Thisobject: This object that is defined when the callback function is executed.
Function description
Each element in an array executes a specified function (callback), and a new array is created for the element each time the result is returned. It executes the specified function only for non-null elements in the array, and the elements that are not assigned or have been deleted are ignored.
The callback function can have three parameters: the current element, the index of the current element, and the current array object.
Look at the example below
The code is as follows |
Copy Code |
<script language= "JavaScript" type= "Text/javascript" > if (! ARRAY.PROTOTYPE.MAP) {Array.prototype.map=function (fun) { 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;};} var strings=["Hello", "Array", "World"];function makeuppercase (v) {return v.touppercase ();} var uppers=strings.map (makeuppercase); Document.writeln ("[" Hello "," Array "," World "].map (makeuppercase):<strong>); Document.writeln (Uppers.join (",")); Document.writeln ("</strong><br/>"); </script> var strings = ["Hello", "Array", "World"]; function MakeUpperCase (v) { return V.touppercase (); } var uppers = Strings.map (makeuppercase); Uppers is now ["HELLO", "ARRAY", "World"] Strings is unchanged |