Skip to content

Suggestion: Create util for making array items unique #3

@MB-web

Description

@MB-web

Found this in one of my projects. Probably this can be done better and escpecially more performant.

/**

  • Produces a duplicate-free version of an object array, using new Set() for test equality.
  • Not meant to be used for array which items are of primitive type.
    */
    export default function(input: T[]): T[] {
    if (!Array.isArray(input)) {
    return [];
    }

return [...new Set(input.map(element => JSON.stringify(element)))].map(element => JSON.parse(element)) as T[];
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions