Unique
Gets the unique values of an array.
๐ฌ Usage
<script>
import {unique} from "@sveu/shared/lists"
const list = [
{id: 1, name: "John"},
{id: 2, name: "Jane"},
{id: 3, name: "Mila"},
{id: 4, name: "Emma"},
{id: 5, name: "Jack"},
{id: 6, name: "Jack"},
]
const unique_names = unique(list, item => item.name) // [{id: 1, name: "John"}, {id: 2, name: "Jane"}, {id: 3, name: "Mila"}, {id: 4, name: "Emma"}, {id: 5, name: "Jack"}]
</script>
๐ฉโ๐ปAPI
๐ป Arguments
Name | Description | Type | Required |
---|---|---|---|
list | The list to get the unique values from | T[] | Yes |
fn | To map items inside list | (item: T) => K | No |
โฉ๏ธ Returns
A list of unique values.
๐งช Playground
Source Code ๐
Source Code
/**
* Gets the unique values of an array.
*
* @param list - The array to process.
*
* @param fn - The function to determine field values
*
* @returns The new array of unique values.
*/
export function unique<T, K extends string | number | symbol>(
list: T[],
fn: (item: T) => K
) {
const value_map = list.reduce((acc, item) => {
const key = fn ? fn(item) : (item as string | number | symbol)
if (acc[key]) return acc
return { ...acc, [key]: item }
}, {} as Record<string | number | symbol, T>)
return Object.values(value_map)
}
Last update: 2023-02-14
Authors: Mohamed-Kaizen