JS: Array.prototype.map() & parseInt()
6795 ワード
Array.prototype.map()メソッドとparseInt()メソッドの組み合わせの使用の深い反省:基礎が重要
1.次のコードの実行結果を書き出します.
console print: Active code page: 65001 ERROR: The process "node.exe"not found. [ 1, NaN, NaN ] [Finished in 0.6s]
Array.prototype.map()
The
JavaScript Demo: Array.map()
Syntax
var new_array = arr.map(function callback(currentValue[, index[, array]]) {//Return element for new_array }[, thisArg])
Parameters callback Function that produces an element of the new Array, taking three arguments: currentValue: The currents element being processed in the array. index/Optional The index of the current element being processed in the array. array/Optional The array map was called upon.
thisArg/Optional Value to use as this when executing callback.
Return value A new array with each element being the result of the callback function.
Description
If a
The range of elements processed by
Due to the algorithm defined in the specification if the array which map was called upon is sparse, resulting array will also be sparse keeping same indices blank.
parseInt()
The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).
JavaScript Demo: parseInt()
Syntax
parseInt(string, radix);
Patameters string The value to parse. If the string argument is not a string, then it is converted to a string(using the ToString abstract operation). Leading whitespace in the string argument is ignored. radix An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string.
Return value
An integer number parsed from the given string. If the first character cannot be converted to a number, NaN is returned.
Description
The
If
Because some numbers include the
If radix is If the input If the input If the input
If the first character cannot be converted to a number,
For arithmetic purposes, the
To convert number to its string literal in a particular radix use
1.次のコードの実行結果を書き出します.
var arr = ["1", "2", "3"].map(parseInt);
console.log(arr);
console print: Active code page: 65001 ERROR: The process "node.exe"not found. [ 1, NaN, NaN ] [Finished in 0.6s]
Array.prototype.map()
The
map()
method creates a new array with the results of calling a provided function on every element in the calling array.map()メソッドは、呼び出し配列内の各要素で提供された関数を呼び出す新しい配列を作成します.JavaScript Demo: Array.map()
var array1 = [1, 4, 9, 16];
// pass a function to map
const map1 = array1.map(x => x * 2);
console.log(map1);
// expected output: Array [2, 8, 18, 32]
Syntax
var new_array = arr.map(function callback(currentValue[, index[, array]]) {//Return element for new_array }[, thisArg])
Parameters
Return value
Description
map
calls a provided callback
function once for each element in an array, in order, and constructs a new array from the results. callback
is invoked only for indexes of the array which have assigned values, including undefined. It is not called for missing elements of the array (that is, indexes that have never been set, which have been deleted or which have never been assigned a value). callback
is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed. If a
thisArg
parameter is provided to map
, it will be used as callback's this
value. Otherwise, the value undefined
will be used as its this
value. The this
value ultimately observable by callback
is determined according to the usual rules for determining the this
seen by a function. map
does not mutate the array on which it is called (although callback
, if invoked, may do so). The range of elements processed by
map
is set before the first invocation of callback
. Elements which are appended to the array after the call to map
begins will not be visited by callback
. If existing elements of the array are changed, their value as passed to callback
will be the value at the time map
visits them. Elements that are deleted after the call to map
begins and before being visited are not visited. Due to the algorithm defined in the specification if the array which map was called upon is sparse, resulting array will also be sparse keeping same indices blank.
parseInt()
The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).
JavaScript Demo: parseInt()
function roughScale(x, base) {
var parsed = parseInt(x, base);
if (isNaN(parsed)) { return 0 }
return parsed * 100;
}
console.log(roughScale(' 0xF', 16));
// expected output: 1500
console.log(roughScale('321', 2));
// expected output: 0
Syntax
parseInt(string, radix);
Patameters
Return value
An integer number parsed from the given string. If the first character cannot be converted to a number, NaN is returned.
Description
The
parseInt
function converts its first argument to a string, parses it, and returns an integer or NaN
. If not NaN
, the returned value will be the integer that is the first argument taken as a number in the specified radix (base). For example, a radix of 10 indicates to convert from a decimal number, 8 octal, 16 hexadecimal, and so on. For radices above 10
, the letters of the alphabet indicate numerals greater than 9
. For example, for hexadecimal numbers (base 16), A
through F
are used. If
parseInt
encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt
truncates numbers to integer values. Leading and trailing spaces are allowed. Because some numbers include the
e
character in their string representation (e.g. 6.022e23
), using parseInt
to truncate numeric values will produce unexpected results when used on very large or very small numbers. parseInt
should not be used as a substitute for Math.floor()
. If radix is
undefined
or 0 (or absent), JavaScript assumes the following: string
begins with "0x"or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed. string
begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt
. string
begins with any other value, the radix is 10 (decimal). If the first character cannot be converted to a number,
parseInt
returns NaN
. For arithmetic purposes, the
NaN
value is not a number in any radix. You can call the isNaN
function to determine if the result of parseInt
is NaN
. If NaN
is passed on to arithmetic operations, the operation results will also be NaN
. To convert number to its string literal in a particular radix use
intValue.toString(radix)
.