Function distinctBy
- distinct
By <T, D>(array, discriminator): T[] Type Parameters
Parameters
Returns T[]
An array of distinct elements in the input array.
Example: Basic usage
import { distinctBy } from "@std/collections/distinct-by";
import { assertEquals } from "@std/assert";
const users = [{ id: 1, name: "Anna" }, { id: 2, name: "Kim" }, { id: 1, name: "Anna again" }];
const uniqueUsers = distinctBy(users, (user) => user.id);
assertEquals(uniqueUsers, [{ id: 1, name: "Anna" }, { id: 2, name: "Kim" }]);
Returns all elements in the given array that produce a unique value using the given discriminator, with the first matching occurrence retained.
Uniqueness is determined by same-value-zero equality of the returned values.