["1", "2", "3"].map (parseint)?

Source: Internet
Author: User

["1", "2", "3"].map (parseint) Get what?

The answer is: [1, Nan, Nan].

Cause: parseint receives two parameters, and map passes 3 parameters.

The map function defines :

Arr.map (Callback[,thisarg]);

The callback function requires the following 3 values:

CurrentValue: Current processing value
Index: The indexed value of the numeric value processed
function values handled by the ARRAY:MAP function

Thisarg is optional. The this value in the callback function defaults to the Window object

parseint function Definition:

parseint (stringradix);

String: The character that needs to be converted, if not the string will be converted, ignoring whitespace.

Radix: integer type before the number 2-36. By default, 10 is used, which indicates decimal.

It is important to note that radix returns Nan if it is outside 2-36.

Execute the following function:

var function (String, Radix, others) {    return string + '-' + radix + '-' + others;}; [' 1 ', ' 2 ', ' 3 '].map (parseint);

Get:

If you put it in the parseint function:

parseint (' 1 ', 0);p arseint (' 2 ', 1);p arseint (' 3 ', 2);

So get 1,nan,nan

Other uses of the map function:

Example 1: Traversing a Queryselectorall object and executing related functions

var elems = Document.queryselectorall (' div '); var function (elem) {    return  elem;});

Example 2: Reversing a string

var str = ' 12345 'function(x) {    return  x;}). Reverse (). Join ();

Attached: This method can judge Palindrome

var str = ' 12345 '; var function (x) {    return  x;}). Reverse (). Join (); if (str = = = str1) {   console.log ('true 'else  {   console.log (' false ') );}

["1", "2", "3"].map (parseint)?

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.