js题解1、arr.map(parseInt)

昨天看到了道题

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

没能理解,今天发现了很好用的DevDocs,查函数查的飞起,终于搞明白了问题:

1、array.map()函数 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).

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,196评论 0 33
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 129,412评论 18 137
  • 笑尽苍生无怨尤, 一生独来也无求。 举目春秋三十载, 闲观江水任东流。 备注: 1、“笑尽苍生”为倒装句,意为“被...
    苦寂生阅读 145评论 0 3
  • 文:秋宇 我听见了海的声音 一个呼啸 我看见了海的颜色 连着天空一直蔓延 很美 我有一个愿望 游到大海与蓝天的交界...
    禾火宇阅读 35评论 0 1