Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidation behavior #18

Open
gibson042 opened this issue Sep 10, 2020 · 6 comments
Open

Consolidation behavior #18

gibson042 opened this issue Sep 10, 2020 · 6 comments
Labels
question Further information is requested

Comments

@gibson042
Copy link

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; // ???
@TechQuery TechQuery added the question Further information is requested label Sep 10, 2020
@TechQuery
Copy link
Collaborator

The polyfill v0.3.0 keeps the first equal element, as what [...new Set([])] does:

if (!map.has(key)) map.set(key, item);

@hax
Copy link
Member

hax commented Sep 10, 2020

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 hax mentioned this issue Sep 10, 2020
@michaelficarra
Copy link
Member

@hax

It seems most userland libs keep first.

Can you post your full findings?

@hax
Copy link
Member

hax commented Sep 12, 2020

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.

@GantMan
Copy link

GantMan commented Sep 20, 2020

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.

@Rudxain
Copy link

Rudxain commented Apr 13, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

6 participants