proposal-array-unique icon indicating copy to clipboard operation
proposal-array-unique copied to clipboard

Consolidation behavior

Open gibson042 opened this issue 5 years ago • 6 comments

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; // ???

gibson042 avatar Sep 10 '20 15:09 gibson042

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

TechQuery avatar Sep 10 '20 16:09 TechQuery

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 avatar Sep 10 '20 18:09 hax

@hax

It seems most userland libs keep first.

Can you post your full findings?

michaelficarra avatar Sep 11 '20 14:09 michaelficarra

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.

hax avatar Sep 12 '20 16:09 hax

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.

GantMan avatar Sep 20 '20 18:09 GantMan

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

Rudxain avatar Apr 13 '23 04:04 Rudxain