I wrote the explanation of this problem last time, and it’s a little tedious, so let me rewrite it

1. The title is as follows

let arr = [1.2.3.4];
arr = arr.map(parsetInt);
console.log(arr);
Copy the code

2. The analysis

arr = arr.map(parsetInt); ParseInt parseInt parseInt parseInt parseInt parseInt parseInt parseInt parseInt

[].map()Is a method on an array prototype that iterates through each item in an array and supports a return value. Such as:

[1.2.3].map(item= >{
  return item*2;   / / minus [2]
});
Copy the code

Specific map method: MDN explanation

ParseInt (parseInt) parseInt (parseInt) parseInt (parseInt) parseInt (parseInt) parseInt (parseInt) parseInt The current item and the corresponding index, and the result of parseInt is replaced by the current item in the array, finally returned as a new array, the original array unchanged

let arr = [1.2.3.4];
arr = arr.map(parsetInt);
console.log(arr);

/* * arr = arr.map(parsetInt) => * arr = arr.map((item,index)=>{ * return parseInt(item,index) * }); * /
Copy the code

parseInt(item,index)

It supports two arguments: parseInt([value],[radix]) :

  1. [value] must be of string type. If not, the value is converted to string by default.
  2. The second parameter is the radix of a base (between 2 and 36), treats [value] as [radix] base, and finally converts to a base 10 number;
  • The second argument is not written, default is 10, but if [value] is a string starting with 0x, default [radix] is 16
  • [Radix] Setting 0 is the same as setting 10, but other than that, NaN is returned as long as it is not between 2 and 36

ParsetInt’s understanding:

  1. The first parameter in parseInt is the basic data type
parseInt('12px');
/* * => The second parameter defaults to 10: ParseInt ('12px',10) * => select * from string ('12px',10) * => select * from string ('12px',10) * > '12' * => Converts the found result to a base 10 number * => Final result: 12 */ 
Copy the code
 parseInt('12px'.0) = >12   // The second argument is 0, which is the same as 10, so use base 10
 parseInt('12px'.1) = >NaN  // The second argument is 1, not between 2 and 36, so it is NaN
Copy the code
  1. The first argument in parseInt is the reference data type
parseInt([10.20.30])
// Replace the array with a string
// => parseInt('10,20,30',10)
// Find the content in base 10
/ / = > '10'
// Finally convert to base 10
/ / = > 10
Copy the code

Convert other bases to binary and decimal

  1. Divide by 2 and take the remainder. Divide by 2 and take the remainder. Step by step operation, until the result of division is zero, and finally all the remainder can be joined together backwards
// Convert n to binary
let n = 32;
/ * * 32/2 = > 16 remainder: 0 * 16/2 = > 8 remainder: 0 * 8/2 = > 4 remainder: 0 * 4/2 = > 2 remainders: 0 * 2/2 = > 1 remainder: 1/2 = > 0 0 * remainder: 1 * To concatenate all the values backwards is the value converted to binary * (32).toString(2) this is also directly converted to binary => "100000" */
Copy the code

2. Convert other base N to base 10: according to the weight of the current bit, calculate the corresponding square to find the result

let n = 2057; // Octal => decimal
// 2*8^3 + 0*8^2+ 5*8^1 + 7*8^0
// 1024 + 0 + 40 + 7 => 1071
Copy the code
let n = '23AF'; // Convert hexadecimal to decimal
// 2*16^3 + 3*16^2 + A(10)*16^1 + F(15)*16^0
// 8192 + 768 + 160 + 15 =>9135
Copy the code
let n = 12.21; // Convert ternary to decimal
//1*3^1 + 2*3^0 + 2*3^-1 + 1*3^-2
/ / 3 (1/3) + 1 + 2 + 2 * * (1 / (3 * 3)) = > 5.777777777777778
Copy the code

Return to the topic

/* * parseInt(1, 1) => 1 => NaN => 1 Return NaN * parseInt(3,2) => NaN => There are no binary values in the string * parseInt(4,3) => parseInt('4',3) => NaN => There is no base 3 */ in the string
Copy the code

3. Extended questions (to see if you really understand the type)

let arr = [10.18.0.10.25.23];
arr = arr.map(parseInt);
console.log(arr);
Copy the code

Steps:

  • Determine the second argument (0 and 10 are both base 10, 2 to 36 are the corresponding bases, and NaN is returned as long as it is not between 2 and 36)
  • Find a value that matches the base of the second argument (stop looking if it doesn’t)
  • Convert it to a numeric type
  • Conversion to decimal (using base conversion)
  • The output
/ * * parseInt (10.18, 0) = > parseInt (' 10.18, 10) = > parseInt (10, 10) = > 10 * parseInt (0, 1) = > NaN * parseInt (10, 2) = > ParseInt (' 10 ', 2) = > parseInt (' 10 ', 2) = > 0 + 1 * 2 * 2 ^ 0 ^ 2 * 1 = > parseInt (25, 3) = > parseInt (' 25 ', 3) = > parseInt (' 2 ', 3) = > 2 * 2 * 3 ^ 0 = > parseInt (23, 4) = > parseInt (' 23 ', 4) = > parseInt (' 23 ', 4) = > 3 * 4 ^ 2 * 4 ^ 0 + 1 = > 11 * /
Copy the code