This article illustrates the use of parseint () and map () in JS. Share to everyone for your reference, specific as follows:
A few examples of parseint ()
var B = parseint ("a");
Alert ("b=" +b);
var c = parseint ("09/08/2009");
Alert ("c=" +c);
Result: b=1,c=0
parseint () is used to parse strings, returning integers, with several features:
1. Ignoring the first and last spaces of the parsed string, that is: "111" and "111" are the same
2. The formal format is parseint (String,radix).
The first argument is the parsed string, and the second argument is the system (not some)
The characteristics of the second parameter:
① if Radix is omitted or 0, it is parsed by default (decimal)
②radix is an integer between 2~36, if outside this range, returns Nan
③string the first character is 0, the default resolution is 8. A string with a first character of 0x (0X) resolves to 16 by default. In other cases, the default is 10.
④ It is best to add a second parameter, otherwise, even if you encounter the first character is 0, should be in 8, in some browsers, such as FF will also be in the 10 system
3. Parsing begins with the first character that can be parsed, to the first unresolved character (such as spaces, punctuation, and so on). The characters that follow are no longer parsed. Returns Nan if the first character cannot be resolved.
Back to this question to analyze
The first letter of B is 0, and the following value is parsed by 8, which is 1 of 8. The natural return value is 1. The first letter of the <br>c is 0, and the following value is parsed by 8, but the second character 9 is not a 8-digit number, that is, 9 is the first unresolved character, parseint ("09/08/2009") is equivalent to parseint ("0") and the natural return value is 0 <br> Note: 8 is not a 8 number, the maximum to 7
Deformation:
var c = parseint ("0119/08/2009");
Alert ("c=" +c);
Analysis:
Equivalent to parseint ("011"), 0*8^2+1*8^1+1*8^0=0+8+1=9, output to c=9
Parsefloat () and parseint ()
parseint () can have parameters, specify binary, octal, or hexadecimal (the default decimal), and parsefloat () without parameters, cannot parse octal, hexadecimal numbers, only in decimal.
Let's look at the parseint () and. Map () combination of examples
["1", "2", "3"].map (parseint) What is the return value?
Map method
Array1.map (callbackfn[, Thisarg])
Each element of an array, call the defined callback function, and return a list containing the result
Parameters:
Array1 |
Have to. An Array object |
Callbackfn |
Have to. Accept up to three arguments (the value of an array element, the index of an array element, the array object that contains the element) |
Thisarg |
Optional. |
The original example is equivalent to
[Parseint ("1", 0), parseint ("2", 1), parseint ("3", 2)]
Analysis:
1 Convert output 1 in decimal
1 not between 2~36, output Nan
There is no valid binary number in the string "3", and the output Nan
So the final result is [1,nan,nan]
More readers interested in JavaScript-related content can view this site: "JavaScript Mathematical operational Usage Summary", "JavaScript data structure and algorithm skills summary", "javascript array operation skills Summary", " JavaScript sorting algorithm summary, JavaScript traversal algorithm and tips summary, JavaScript search algorithm Skills summary and JavaScript error and Debugging skills summary
I hope this article will help you with JavaScript programming.