Function unique

  • Gets only the unique elements of an array.

    Type Parameters

    • T extends readonly unknown[]

    Parameters

    • array: T

      The array to search.

    Returns T

    A new array containing only the unique elements.

    Remarks

    This method filters duplicates by inserting all elements into a Set. Therefore, each element is compared using same-value-zero equality, which has the same behavior as Object.is, except +0 and -0 are considered equal.

    Example

    Basic Usage

    unique([1, 2, 3, 2, 1]) // [1, 2, 3]
    unique([0, -0, 1]) // [0, 1]
    unique([NaN, Number.NaN, NaN]) // [NaN]

    Objects are compared by reference.

    const foo = { a: 1 }
    unique([foo, foo]) // [foo]
    const bar = { a: 1 }
    unique([foo, bar]) // [foo, bar]

Generated using TypeDoc