Function removeDuplicates

  • Removes duplicates from an array. Optionally provide a callback for calculating a unique id for entries.

    Type Parameters

    • const Entry

    Parameters

    • originalArray: readonly Entry[]
    • calculateUniqueId: (entry: Readonly<Entry>) => unknown = ...

    Returns Entry[]

    A new array (does not mutate).

    import {removeDuplicates} from '@augment-vir/common';

    const result = removeDuplicates([1, 1, 1, 1, 1, 2, 4]);
    // result is `[1, 2, 4]`

    const exampleEntry = {id: 5};

    const result2 = removeDuplicates([
    {id: 1},
    // this entry will not get filtered out because it's a new object reference
    {id: 1},
    exampleEntry,
    // this `exampleEntry` will get filtered out because it's the same reference as the one above
    exampleEntry,
    {id: 4},
    ]);
    // result2 is `[{id: 1}, {id: 1}, exampleEntry, {id: 4}]`
    import {removeDuplicates} from '@augment-vir/common';

    const exampleEntry = {id: 5};

    const result2 = removeDuplicates(
    [{id: 1}, {id: 1}, exampleEntry, exampleEntry, {id: 4}],
    (entry) => entry.id,
    );
    // result2 is `[{id: 1}, exampleEntry, {id: 4}]`