shlogg · Early preview
Ramu Narasinga @karthik-m22

Creating Unique Array In JavaScript With New Set()

Create unique array in JavaScript using new Set() and Array.from(). Inspired by TypeDoc's code reference. Example: `const unique = Array.from(new Set([1,2,3,1,2,5,4,3]))

In this article, we analyze how you can create a unique array using new set in JavaScript, inspired by a code reference from TypeDoc.

Let’s first understand this unique function in the above image with an example and then we will look at how this is used in TypeDoc source code.

  
  
  unique function


export function unique<T>(arr: Iterable<T> | undefined): T[] {
    return Array.from(new Set(arr));
}

    
    

    
    




This function is picked from typedoc/src/lib/utils/array.ts.
Array.from static method creates a new, shallow-copied Array instance from an iterable or array-like obj...