js題解1、arr.map(parseInt)

昨天看到了道題

["1","2","3"].map(parseInt) => ?

沒能理解,今天發(fā)現(xiàn)了很好用的DevDocs,查函數(shù)查的飛起,終于搞明白了問題:

1、array.map()函數(shù) y = f (x)

The map() method creates a new array with the results of calling a provided function on every element in this array.

Syntax
var new_array = arr.map( callback [ ,thisArg ] )

Parameters
callback : currentValue, index, array;
hisArg ? this : undefined

Return value
A new array with each element being the result of the callback function.

//examples:

var numbers = [1, 5, 10, 15];
var roots = numbers.map(function(x) {
   return x * 2;
});
// roots is now [2, 10, 20, 30]
// numbers is still [1, 5, 10, 15]

var numbers = [1, 4, 9];
var roots = numbers.map(Math.sqrt);
// roots is now [1, 2, 3]
// numbers is still [1, 4, 9]

more details and examples.

2、parseInt

The parseInt() function parses(解析) a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).

Syntax
parseInt(string, radix);

Parameters
string : 需要解析的字符串,if not a string ,use ToString => string
radix : An integer between 2 and 36

Return value
An integer number parsed from the given string. If the first character cannot be converted to a number, NaN
is returned.

If radix is undefined or 0 (or absent), JavaScript assumes the following:

If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed.
If the input 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.
If the input string begins with any other value, the radix is 10 (decimal).
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 背景 一年多以前我在知乎上答了有關LeetCode的問題, 分享了一些自己做題目的經(jīng)驗。 張土汪:刷leetcod...
    土汪閱讀 12,931評論 0 33
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,688評論 19 139
  • 笑盡蒼生無怨尤, 一生獨來也無求。 舉目春秋三十載, 閑觀江水任東流。 備注: 1、“笑盡蒼生”為倒裝句,意為“被...
    苦寂生閱讀 322評論 0 3
  • 文:秋宇 我聽見了海的聲音 一個呼嘯 我看見了海的顏色 連著天空一直蔓延 很美 我有一個愿望 游到大海與藍天的交界...
    禾火宇閱讀 115評論 0 1

友情鏈接更多精彩內容