Skip to content

unique

Remove duplicates from an array

159 bytes

Usage

Given an array of items — and optionally, a function to determine their identity — return a new array without any duplicates.

The function does not preserve the original order of items.

import * as
import _
_
from 'radashi'
const
const fish: {
name: string;
weight: number;
source: string;
}[]
fish
= [
{
name: string
name
: 'Marlin',
weight: number
weight
: 105,
source: string
source
: 'ocean',
},
{
name: string
name
: 'Salmon',
weight: number
weight
: 22,
source: string
source
: 'river',
},
{
name: string
name
: 'Salmon',
weight: number
weight
: 22,
source: string
source
: 'river',
},
]
import _
_
.
unique<{
name: string;
weight: number;
source: string;
}, string>(array: readonly {
name: string;
weight: number;
source: string;
}[], toKey?: ((item: {
name: string;
weight: number;
source: string;
}) => string) | undefined): {
name: string;
weight: number;
source: string;
}[]
export unique

Given a list of items returns a new list with only unique items. Accepts an optional identity function to convert each item in the list to a comparable identity value.

@seehttps://radashi.js.org/reference/array/unique

@example

unique([1, 1, 2, 2]) // => [1, 2]
unique([1, 2, 3], (n) => n % 2) // => [1, 2]

@version12.1.0

unique
(
const fish: {
name: string;
weight: number;
source: string;
}[]
fish
,
f: {
name: string;
weight: number;
source: string;
}
f
=>
f: {
name: string;
weight: number;
source: string;
}
f
.
name: string
name
)
// [
// { name: 'Marlin', weight: 105, source: 'ocean' },
// { name: 'Salmon', weight: 22, source: 'river' }
// ]