The JavaScript map () function converts all array elements to uppercase

Source: Internet
Author: User

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

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.