The first step would be to convert the original array to a Set object.
To create a new Set object simply use the syntax “new Set” followed by the original array as an argument in parentheses as shown above. The new Set object returns only unique values.
Once you have the Set of unique values you can then create a new array and add the Set values to it using the spread operator (…setObject).
And there you have your new array of unique values.
An even more condensed way of creating a unique array is to use Array.from. This consolidates the two steps into one.
Simply create a new array variable name and set it to Array.from with “new Set” as the argument and the original array as the argument of the new Set.
In addition to creating an object of unique values there are several functions that can be called on a Set.
A value can be added to a Set using the “add” function and passing in the new value as an argument in parentheses.
New values can be added to the Set but they must be unique.
The “has” method allows you to check if the value is in the set.
A couple other methods are “size” and “delete”. Size returns the length of the Set and delete removes a value from the set.
Set can also be used on strings. Similar to the “split” method it divides the string into individual values by character. With strings the unique filtering is case sensitive so if a capitol and lowercase version of the same letter exist they will both be included in the set.
In the first example the letter “s” is included twice because one is uppercase and the other is lowercase. In the second example the second “s” is removed because they are both lowercase.
In a case where there is an array of strings it will simply separate by the index values of the array.
The uniqueness would still be case sensitive. The second “two” is removed but “Two” is included because it is uppercase.