uniqueArrayByKey

Removes duplicate objects from an array based on a specific key and returns an array with unique objects.
> uniqueArrayByKey<T>({ array, key }: UniqueArrayByKeyParams<T>): T[]
Parameters
  • array: An array of any type `T` to be de-duplicated.
  • key: A `string` representing the key in each object by which uniqueness is determined.
  • Returns:A new array containing objects with unique values based on the specified key.

Example

import { uniqueArrayByKey } from "@explita/daily-toolset";

const people = [
    { id: 1, name: "Alice" },
    { id: 2, name: "Bob" },
    { id: 1, name: "Alice" },
];

console.log(uniqueArrayByKey({ array: people, key: "id" })); 
// [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }]