Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ECMAScript proposal for Deduplicating method of Array

NotificationsYou must be signed in to change notification settings

tc39/proposal-array-unique

ECMAScript proposal for Deduplicating method of Array.

Proposal Stage-1CI & CD

NPM

Motivation

Deduplication is one of the most common requirements in Data processing, especially in large Web Apps nowadays.

In Lodash,*uniq* methods are alsovery popular:

#NameDownloads
1uniq5,546,070
7uniqby447,858
28uniqwith15,077

But[...new Set(array)] in ECMAScript 6 isn't enough forNon-primitive values, and now, we may need aArray.prototype.uniqueBy().

Core features

WhileArray.prototype.uniqueBy() invoked with:

  1. no parameter, it'll work as[...new Set(array)];

  2. oneindex-key parameter (Number,String orSymbol), it'll get values from each array element with the key, and then deduplicates the origin array based on thesevalues;

  3. onefunction parameter, it'll call this function for each array element, and then deduplicates the origin array based on thesereturned values.

Notice:

  • theReturned value is a new array, no mutation happens in the original array
  • Empty/nullish items are treated as nullish values
  • 0 &-0 are treated as the same
  • AllNaNs are treated as the same

Typical cases

[1,2,3,3,2,1].uniqueBy();// [1, 2, 3]constdata=[{id:1,uid:10000},{id:2,uid:10000},{id:3,uid:10001}];data.uniqueBy('uid');// [//     { id: 1, uid: 10000 },//     { id: 3, uid: 10001 }// ]data.uniqueBy(({ id, uid})=>`${id}-${uid}`);// [//     { id: 1, uid: 10000 },//     { id: 2, uid: 10000 },//     { id: 3, uid: 10001 }// ]

Polyfill

A polyfill is available in thecore-js library. You can find it in theECMAScript proposals section.

A simple polyfill from the proposal repo write in TypeScript.

Proposer

About

ECMAScript proposal for Deduplicating method of Array

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors3

  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp