@andranik-arakelyan/js-utilities
Version:
Javascript utilities
24 lines (23 loc) • 861 B
TypeScript
/**
* Returns a new array with duplicate values removed.
*
* @template T The type of elements in the array
* @template K The type of the key used for uniqueness comparison
* @param array The input array to remove duplicates from
* @param getKey Optional function to determine uniqueness key for each item
* @returns A new array with only unique values
*
* @example
* // Simple usage with primitive values
* unique([1, 2, 2, 3, 1, 4]); // [1, 2, 3, 4]
* unique(['a', 'b', 'a', 'c']); // ['a', 'b', 'c']
*
* // With objects using a key selector
* const users = [
* { id: 1, name: 'Alice' },
* { id: 2, name: 'Bob' },
* { id: 1, name: 'Alice (duplicate)' }
* ];
* unique(users, user => user.id); // [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]
*/
export declare function unique<T, K = T>(array: T[], getKey?: (item: T) => K): T[];