ConstElement type produced by the source iterable.
Key type returned by the selector.
Selector producing a comparison key for each element.
Optional factory supplying the set used to track seen keys.
A deferred iterable yielding the first element for each unique key.
const users = [
{ id: 1, name: "Ada" },
{ id: 1, name: "Ada" },
{ id: 2, name: "Grace" },
];
const unique = [..._distinctBy(users, (user) => user.id)];
console.log(unique); // [{ id: 1, name: "Ada" }, { id: 2, name: "Grace" }]
or using the curried version:
const unique = [
...pipeInto(
[
{ id: 1, name: "Ada" },
{ id: 1, name: "Ada" },
{ id: 2, name: "Grace" },
],
distinctBy((user) => user.id)
),
];
console.log(unique); // [{ id: 1, name: "Ada" }, { id: 2, name: "Grace" }]
Emits source elements while removing duplicates identified by the selector key.
Curried version of _distinctBy.