【问题标题】:Using .map without creating a new array使用 .map 而不创建新数组
【发布时间】:2020-05-11 00:45:42
【问题描述】:

假设我有这个数据:

const celsius = [-15, -5, 0, 10, 16, 20, 24, 32]
const fahrenheit = celsius.map(t => t * 1.8 + 32);

所以celsius 是一个数组,而fahrenheit 正在使用map,即使用celsius 创建另一个具有以下值的数组:

>[0]
    >5
    >23
    >32
    >50
    >60.8
    >68
    >75.2
    >89.6

我的问题是它创建的新数组,因为我只需要数组的值,而数组 [0] 使得使用它变得非常困难。所以我的问题是:我可以用.map 创建一个数组而不创建数组[0] 吗?

【问题讨论】:

  • 我想你很困惑。 fahrentheit 只包含数字:5、23 等。元素数量与摄氏度数组相同。
  • 不清楚你在问什么。你想达到什么目标?

标签: arrays angular array.prototype.map


【解决方案1】:

来自 MDN -

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

我不知道你是怎么得到那个输出的,但是 map 总是创建一个新的 array

所以回答你不。

【讨论】:

  • 谢谢,我去寻找其他解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多