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/proposal-array-filtering.md.
  • 简体中文
  • Array filtering S1

    中文标题:数组过滤

    提案概览
    提案速览

    该提案解决了 Array.prototype.filter 语义带来的困惑,‘filter’ 暗示移除但实际保留项。它提议添加 Array.prototype.filterReject,它会删除回调返回 true 的项,符合‘过滤掉’的直觉含义。

    Note

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

    proposal-array-filtering

    一个添加 Array.prototype.filterReject 的提案。

    const array = [1, 2, 3, 4, 5];
    
    // filter 保留返回 true 的项。
    array.filter(i => (i < 3)); // => [1, 2];
    
    // filterReject 删除返回 true 的项。
    array.filterReject(i => (i < 3)); // => [3, 4, 5];

    拥护者

    状态

    当前 阶段: 1

    动机

    Array.p.filter 令人困惑。我经常不得不问自己:“我是保留当前项,还是过滤掉当前项?”。

    “保留”

    意味着返回 true 会保留当前项。

    “过滤掉”

    意味着返回 true 会移除当前项。

    Array.p.filter 的行为是“保留”。但当我想到“过滤”这个词时,我想到的是“过滤掉”。所以每次我尝试编写数组过滤时,最终都会写成与我的意图相反的内容。

    Array.p.filterReject 试图解决这种困惑。通过提供一个命名清晰且符合直觉的过滤函数,我能够知道调用 filterReject 时会发生什么。并且因为它的存在,我可以假设 filter 做了不同的事情,所以它必须是“保留”版本。

    Polyfill

    core-js 库中提供了 polyfill。你可以在 ECMAScript proposals section 找到它。

    持续讨论

    相关