JS: Array.prototype.map() & parseInt()

Array.prototype.map()方法與parseInt()方法的結(jié)合使用的深入反思:基礎(chǔ)很重要

1. 寫出下面代碼的執(zhí)行結(jié)果:

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()方法創(chuàng)建一個新數(shù)組轴合,其結(jié)果是在調(diào)用數(shù)組中的每個元素上調(diào)用提供的函數(shù)

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

  • 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

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

  • 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 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:

  • 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).

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).

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末享扔,一起剝皮案震驚了整個濱河市澈圈,隨后出現(xiàn)的幾起案子杰标,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 210,914評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件台夺,死亡現(xiàn)場離奇詭異,居然都是意外死亡痴脾,警方通過查閱死者的電腦和手機颤介,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,935評論 2 383
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來赞赖,“玉大人滚朵,你說我怎么就攤上這事∏坝颍” “怎么了辕近?”我有些...
    開封第一講書人閱讀 156,531評論 0 345
  • 文/不壞的土叔 我叫張陵,是天一觀的道長匿垄。 經(jīng)常有香客問我移宅,道長,這世上最難降的妖魔是什么椿疗? 我笑而不...
    開封第一講書人閱讀 56,309評論 1 282
  • 正文 為了忘掉前任漏峰,我火速辦了婚禮,結(jié)果婚禮上届榄,老公的妹妹穿的比我還像新娘浅乔。我一直安慰自己,他們只是感情好铝条,可當我...
    茶點故事閱讀 65,381評論 5 384
  • 文/花漫 我一把揭開白布靖苇。 她就那樣靜靜地躺著,像睡著了一般班缰。 火紅的嫁衣襯著肌膚如雪贤壁。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,730評論 1 289
  • 那天鲁捏,我揣著相機與錄音芯砸,去河邊找鬼。 笑死给梅,一個胖子當著我的面吹牛假丧,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播动羽,決...
    沈念sama閱讀 38,882評論 3 404
  • 文/蒼蘭香墨 我猛地睜開眼包帚,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了运吓?” 一聲冷哼從身側(cè)響起渴邦,我...
    開封第一講書人閱讀 37,643評論 0 266
  • 序言:老撾萬榮一對情侶失蹤疯趟,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后谋梭,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體信峻,經(jīng)...
    沈念sama閱讀 44,095評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,448評論 2 325
  • 正文 我和宋清朗相戀三年瓮床,在試婚紗的時候發(fā)現(xiàn)自己被綠了盹舞。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,566評論 1 339
  • 序言:一個原本活蹦亂跳的男人離奇死亡隘庄,死狀恐怖踢步,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情丑掺,我是刑警寧澤获印,帶...
    沈念sama閱讀 34,253評論 4 328
  • 正文 年R本政府宣布,位于F島的核電站街州,受9級特大地震影響兼丰,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜菇肃,卻給世界環(huán)境...
    茶點故事閱讀 39,829評論 3 312
  • 文/蒙蒙 一地粪、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧琐谤,春花似錦、人聲如沸玩敏。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,715評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽旺聚。三九已至织阳,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間砰粹,已是汗流浹背唧躲。 一陣腳步聲響...
    開封第一講書人閱讀 31,945評論 1 264
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留碱璃,地道東北人弄痹。 一個月前我還...
    沈念sama閱讀 46,248評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像嵌器,于是被迫代替她去往敵國和親肛真。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 43,440評論 2 348

推薦閱讀更多精彩內(nèi)容

  • 今天早上調(diào)休爽航,醒的時間和往常一樣很有規(guī)律7:30分蚓让,同樣和往常一樣滑滑手機乾忱,刷刷微博,朋友圈历极。我決定堅持每天跑步窄瘟,...
    迷路的刺猬君閱讀 214評論 1 0
  • 今天是藝林上學的第二天,他的積極向上的心態(tài)讓我感到驕傲趟卸,他讓我把他的運動計步手環(huán)上的鬧鐘設(shè)置好寞肖,分別是早晨六...
    A新華洗化閱讀 213評論 0 1
  • 露冷階前桂,雨侵枝上花衰腌。幽香關(guān)不住新蟆,越院去鄰家。
    飛飛_b4dc閱讀 321評論 2 2
  • 寶寶你好,我是你的媽媽饶囚,之前爸爸給你寫了10封信帕翻,媽媽看了也有很多感觸。所以這周萝风,媽媽也想分享一些自己的感受給你嘀掸,...
    年糕佳爸閱讀 817評論 6 11
  • 下午在看臺大哲學教授苑舉正所著的《哲學六講》睬塌,看到第五講亞里士多德時,書內(nèi)有這樣一句話“有關(guān)變化的過程歇万,亞里士多德...
    山河萬朵閱讀 195評論 0 4