Discuss / JavaScript / 有难度,需要重温

有难度,需要重温

Topic source

zydchina818

#1 Created at ... [Delete] [Delete and Lock User]
  1.  return arr.reduce((x, y) => x*y);

  2. return s.split('').map((x) => x*1).reduce((x, y)=>x * 10 + y);

  3. return arr.map(x => x[0].toUpperCase()+x.substring(1).toLowerCase());

  4. r = arr.map(Number);

我们期望输出 [1, 2, 3], 而实际结果是 [1, NaN, NaN].

parseInt 经常被带着一个参数使用, 但是这里接受两个。第一个参数是一个表达式而第二个是callback function的基, Array.prototype.map 传递3个参数:

  • the element
  • the index
  • the array

zydchina818

#2 Created at ... [Delete] [Delete and Lock User]

map和reduce需要配合MDN好好看一下,有点迷糊。


  • 1

Reply