💎 Creates a unique version of an array by removing duplicate values.

Info

It uses isEql for deep comparison of values.

Syntax

TypeScript
import { unique } from '@opentf/std';

unique<T>(
  arr: T[] = [],
  by?: (val: T) => unknown
): T[]

Parameters

  • arr: The source array.

  • by: An optional iteratee invoked for each element to generate the criterion by which uniqueness is computed.

Returns

A new array containing only unique values.

Examples

TypeScript
const arr = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5];
unique(arr) //=> [1, 2, 3, 4, 5]

const nums = [2.1, 1.2, 2.3];
unique(nums, Math.floor) //=> [2.1, 1.2]
Last updated on
Edit this page