unique

Creates a unique array from the input array.

1.
/**
2.
* Creates a unique array from the input array.
3.
**/
4.
const unique = <T,>(arr: T[]): T[] => {
5.
return [...new Set(arr)];
6.
};
7.
8.
export default unique;

1. Installtion

npx @jrtilak/lazykit add unique

2. Parameters

  • arr (T[])
    The array from which to create a unique set of elements. Duplicate elements will be removed.

3. Returns

  • T[]
    Returns a new array containing only the unique elements from the original array. The order of elements is preserved based on their first appearance.

4. Type Parameters

  • T
    The type of elements in the array. This allows the function to work with arrays of any type, ensuring type safety for the unique elements.

5. Usage

1.
import unique from "@/helpers/unique";
2.
3.
const arr = [1, 2, 3, 3];
4.
const result = unique(arr);
5.
console.log(result);
6.
// Expected Output: [1, 2, 3]