All Projects → tc39 → Proposal Array Filtering

tc39 / Proposal Array Filtering

Licence: mit
A proposal to make filtering arrays easier

Labels

proposal-array-filtering

A proposal to add Array.prototype.filterOut.

const array = [1, 2, 3, 4, 5];

// filter keeps the items that return true.
array.filter(i => (i < 3)); // => [1, 2];

// filterOut removes the items that return true.
array.filterOut(i => (i < 3)); // => [3, 4, 5];

Champions

Status

Current Stage: 1

Motivation

Array.p.filter is confusing. I constantly have to ask myself "am I keeping, or filtering out the current item?".

"Keeping"

Implies that returning true would keep the current item.

"Filtering out"

Implies that returning true would remove the current item.

Array.p.filter acts as "keeping". But when I think of the word "filter", I think of "filtering out". So every time that I attempt to write an array filter, I end up writing the opposite of what I intended.

Array.p.filterOut attempts to fix this confusion. By providing a clearly named filtering function that matches my intuition, I'm able what will happen when calling filterOut. And because it exists, I'm able to assume that filter does something different, so it must be "keep" version.

Polyfill

A polyfill is available in the core-js library. You can find it in the ECMAScript proposals section.

Ongoing Discussions

Related

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].