proposal-array-unique
proposal-array-unique copied to clipboard
Consolidation behavior
Should the unique operation keep the first value with a given surrogate key or the last, and at what index? Is there reason to make it author-configurable?
const data = [
{ id: 1, name: "1a" },
{ id: 2, name: "2a" },
{ id: 1, name: "1b" },
];
data.uniqueBy(v => v.id)[0].name; // ???
The polyfill v0.3.0 keeps the first equal element, as what [...new Set([])] does:
https://github.com/tc39/proposal-array-unique/blob/0df27fa2fc942291d375797bb1d8fe09ef2d8245/polyfill/index.ts#L28
It seems most userland libs keep first.
There are use cases which prefer last. For example, dedup update operations and use the last update. But in such cases, we possible need much more control, for example, in https://stackoverflow.com/questions/27234820/how-to-make-javascript-array-unique-by-attribute the author want keep the item with biggest timestamp. A possible solution is allow second param:
data.uniqueBy(v => v.release, (x, y) => y.timestamp > x.timestamp)
@hax
It seems most userland libs keep first.
Can you post your full findings?
Both lodash, ramda keep first. I also found another two packages in npm (but forgot accurate names now, need to search again) also keep first.
Note, I guess ramda just copied lodash behavior, so other packages may also just copied lodash.
I don't believe the essence of unique is to provide configurability of how to pick values that are unique. To that end a developer can lean on filter and reduce to assure controlling selection. Just like we've seen from previous libraries that have stemmed from lodash, this is probably a justifiably separate concern.
The following is my "2 cents" of personal experience:
There are some cases where keeping the last occurrence is desirable. This is an example in Python
Anything that is a "history" or "log" can benefit from removing old duplicates. Old dupes are usually located at the beginning of a list/array (unless the format is reversed). Therefore I conclude that keeping the last item may prove to be useful in practice