https://github.com/tc39/proposal-array-unique
ECMAScript proposal for Deduplicating method of Array
https://github.com/tc39/proposal-array-unique
array deduplicate ecmascript javascript polyfill proposal unique
Last synced: 4 months ago
JSON representation
ECMAScript proposal for Deduplicating method of Array
- Host: GitHub
- URL: https://github.com/tc39/proposal-array-unique
- Owner: tc39
- Created: 2018-11-23T09:15:52.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-03-16T13:50:41.000Z (about 3 years ago)
- Last Synced: 2024-12-11T10:24:28.844Z (4 months ago)
- Topics: array, deduplicate, ecmascript, javascript, polyfill, proposal, unique
- Language: TypeScript
- Size: 62.5 KB
- Stars: 138
- Watchers: 16
- Forks: 7
- Open Issues: 13
-
Metadata Files:
- Readme: ReadMe.md
Awesome Lists containing this project
README
# Array deduplication proposal
ECMAScript proposal for Deduplicating method of Array.

[][1][][2]
## Motivation
**Deduplication** is one of the most common requirements in Data processing, especially in large Web Apps nowadays.
In Lodash, `*uniq*` methods are also [very popular][3]:
| # | 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 for **Non-primitive values**, and now, we may need a `Array.prototype.uniqueBy()`.
## Core features
While `Array.prototype.uniqueBy()` invoked with:
1. no parameter, it'll work as `[...new Set(array)]`;
2. one **index-key** parameter (`Number`, `String` or `Symbol`), it'll get values from each array element with the key, and then deduplicates the origin array based on these _values_;
3. one **function** parameter, it'll call this function for each array element, and then deduplicates the origin array based on these _returned values_.
Notice:
- the **Returned 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
- All `NaN`s are treated as the same## Typical cases
```JavaScript
[1, 2, 3, 3, 2, 1].uniqueBy(); // [1, 2, 3]const data = [
{ 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 the [core-js][4] library. You can find it in the [ECMAScript proposals section][5].
[A simple polyfill from the proposal repo write in TypeScript.](polyfill/index.ts)
## Proposer
- Author: [@TechQuery](https://github.com/TechQuery)
- Champion: [@Jack-Works](https://github.com/Jack-Works)[1]: https://github.com/tc39/proposal-array-unique/actions/workflows/main.yml
[2]: https://nodei.co/npm/array-unique-proposal/
[3]: https://github.com/AndrewRot/lodash-function-usage/blob/9ec92a7980e321f8a32dd62e13addd6562ba4612/README.md
[4]: https://github.com/zloirock/core-js
[5]: https://github.com/zloirock/core-js#array-deduplication