【问题标题】:What does "Array.from() does not create an intermediate array" mean?“Array.from() 不会创建中间数组”是什么意思?
【发布时间】:2020-03-28 18:07:05
【问题描述】:

来自MDN Web Docs, Array.from(obj, mapFn, thisArg)Array.from(obj).map(mapFn, thisArg) 的结果相同, 除了它不创建中间数组

这对于某些数组子类尤其重要,例如typed arrays,因为中间数组必然有值 被截断以适合适当的类型。

Array.from() 不会创建中间数组”是什么意思?

【问题讨论】:

    标签: javascript arrays


    【解决方案1】:

    你可以将Array.from(obj).map(mapFn, thisArg)分解为

    const arr1 = Array.from(obj);
    const arr2 = arr1.map(mapFn, thisArg);
    

    由于Array.from()Array.prototype.map 都返回了一个新数组,因此已经创建了两个数组。

    Array.from(obj, mapFn, thisArg) 创建一个数组。

    【讨论】:

      猜你喜欢
      • 2015-07-04
      • 2013-10-26
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      • 2017-06-11
      • 2018-03-05
      • 2023-03-27
      • 1970-01-01
      相关资源
      最近更新 更多