Skip to content

unique

Returns a new array with duplicate elements removed. For arrays of objects, you can specify a property to determine uniqueness.

import { unique } from "1o1-utils";
import { unique } from "1o1-utils/unique";
function unique<T>({ array, key }: UniqueParams<T>): T[]
NameTypeRequiredDescription
arrayT[]YesThe array to deduplicate
keykeyof TNoProperty to determine uniqueness for objects

T[] — A new array with duplicates removed.

unique({ array: [1, 2, 2, 3, 1] });
// => [1, 2, 3]
const users = [
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" },
{ id: 1, name: "Alice Copy" },
];
unique({ array: users, key: "id" });
// => [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }]
  • Throws if array is not an array.
  • Without key, uses Set for primitive deduplication.
  • With key, keeps the first occurrence of each unique value.

deduplicate, distinct, uniq, remove duplicates

I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use unique to deduplicate an array of objects by ID