For AI agents: the complete documentation index is available at /tc39-atlas/llms.txt, the full documentation bundle is available at /tc39-atlas/llms-full.txt, and this page is available as Markdown at /tc39-atlas/proposals/stage/1/proposal-array-zip.md.
  • 简体中文
  • Array.zip and Array.zipKeyed S1

    中文标题:Array.zip 和 Array.zipKeyed

    提案概览
    提案速览

    该提案添加了静态方法 Array.zip 和 Array.zipKeyed,用于同步多个数组的迭代,其 API 和语义完全基于 proposal-joint-iteration 中的 Iterator.zip 和 Iterator.zipKeyed。它解决了在小型有限列表中人们普遍偏好使用数组而非迭代器的问题,并相比通过 .toArray() 和 Iterator.from() 转换迭代器更加方便。

    Note

    以下 README 来自上游仓库,其中的阶段或状态标注可能滞后;当前信息以提案概览为准。

    proposal-array-zip

    一个 TC39 提案,用于同步多个数组的迭代。

    本提案完全基于 https://github.com/tc39/proposal-joint-iteration - 换句话说,本提案是 Iterator.zipIterator.zipKeyed 的数组版本。

    阶段:1

    规范https://tc39.es/proposal-array-zip/

    动机 / 问题

    尽管迭代器已经存在了十多年,但使用数组(而非迭代器)仍然极为普遍,尤其是对于小型有限列表。数组更易于操作,通常性能更高,并且与生态系统库的兼容性更广。

    现在,人们总是可以通过 .toArray() 将任何迭代器转换为数组 - 这很有用,但不够方便,尤其是在访问迭代器辅助方法之前需要将值包装在 Iterator.from() 中时。

    解决方案

    Array 构造函数添加 Array.zipArray.zipKeyed 静态方法。

    理由

    • 方法名称、API 和语义必须与 https://github.com/tc39/proposal-joint-iteration 匹配,以避免混淆
    • 这包括接受可迭代对象,例如 Array.fromArray.fromAsync
    • 添加 Array.prototype 方法是导致 Web 兼容性问题最常见的原因,并且浏览器已表示未来甚至不愿意尝试添加更多此类方法 - 因此,它们必须是静态的。