- Notifications
You must be signed in to change notification settings - Fork5
tc39/proposal-array-unique
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
ECMAScript proposal for Deduplicating method of Array.
Deduplication is one of the most common requirements in Data processing, especially in large Web Apps nowadays.
In Lodash,*uniq* methods are alsovery popular:
| # | Name | Downloads |
|---|---|---|
| 1 | uniq | 5,546,070 |
| 7 | uniqby | 447,858 |
| 28 | uniqwith | 15,077 |
But[...new Set(array)] in ECMAScript 6 isn't enough forNon-primitive values, and now, we may need aArray.prototype.uniqueBy().
WhileArray.prototype.uniqueBy() invoked with:
no parameter, it'll work as
[...new Set(array)];oneindex-key parameter (
Number,StringorSymbol), it'll get values from each array element with the key, and then deduplicates the origin array based on thesevalues;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&-0are treated as the same- All
NaNs are treated as the same
[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 }// ]
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.
- Author:@TechQuery
- Champion:@Jack-Works
About
ECMAScript proposal for Deduplicating method of Array
Topics
Resources
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.
