Last updated: Mar 2, 2024
Reading timeยท5 min
for...of
loopUse the Set()
constructor to convert an array to a Set
, e.g.
const set = new Set(arr);
.
The Set()
constructor is used to create Set
objects that store unique
values of any type.
// โ Convert an Array to a Set const arr = ['a', 'a', 'b', 'c', 'c']; // ๐๏ธ {'a', 'b', 'c'} const set = new Set(arr); console.log(set.size); // ๐๏ธ 3 console.log(set.has('c')); // ๐๏ธ true // ------------------------------------------ // โ Convert an Array of Objects to a Set const arr2 = [ {id: 1, letter: 'a'}, {id: 2, letter: 'b'}, {id: 3, letter: 'c'}, ]; const set2 = new Set(arr2.map(obj => obj.letter)); // ๐๏ธ Set(3) { 'a', 'b', 'c' } console.log(set2);
The
Set
object only stores unique values. Even if the supplied array contains
duplicates, they don't get added to the Set
.
const arr = ['a', 'a', 'a']; const set = new Set(arr); console.log(set); // ๐๏ธ Set(1) { 'a' } console.log(set.size); // ๐๏ธ 1
The only parameter the Set() constructor takes is an iterable.
Set()
constructor, all of the array's elements get added to the Set
(without the duplicates).If you need to convert the Set
back to an array, use the
spread syntax (...).
const arr = ['a', 'a', 'b', 'c', 'c']; // ๐๏ธ {'a', 'b', 'c'} const set = new Set(arr); const newArr = [...set]; // ๐๏ธ ['a', 'b', 'c'] console.log(newArr);
The spread syntax (...) unpacks the values of the Set
into the new array.
You can also use the Array.from()
method to convert the Set
back to an
array.
const arr = ['a', 'a', 'b', 'c', 'c']; // ๐๏ธ {'a', 'b', 'c'} const set = new Set(arr); const newArr = Array.from(set); // ๐๏ธ ['a', 'b', 'c'] console.log(newArr);
The Array.from()
method creates an array from the provided iterable.
If you need to convert an array of objects to a Set
containing only the values
of a specific property, use the Array.map()
method.
To convert an Array of objects to a Set
:
Array.map()
method to iterate over the array.Set()
constructor to convert the array to a Set
object.const arr = [ {id: 1, letter: 'a'}, {id: 2, letter: 'b'}, {id: 3, letter: 'c'}, ]; const set = new Set(arr.map(obj => obj.letter)); // ๐๏ธ Set(3) { 'a', 'b', 'c' } console.log(set);
The function we passed to the Array.map() method gets called with each element (object) in the array.
On each iteration, we access the letter
property on the object and return the
result.
const arr = [ {id: 1, letter: 'a'}, {id: 2, letter: 'b'}, {id: 3, letter: 'c'}, ]; // ๐๏ธ ['a', 'b', 'c'] console.log(arr.map(obj => obj.letter)); const set = new Set(arr.map(obj => obj.letter)); // ๐๏ธ Set(3) { 'a', 'b', 'c' } console.log(set);
The output of the Array.map()
method is an array containing all of the values
of the letter
property.
The last step is to pass the array of values to the Set()
constructor.
This is a three-step process:
Set
object.Array.forEach()
method to iterate over the array.Set.add()
method to add the element to the
Set
.const arr = ['a', 'a', 'b', 'c', 'c']; const set = new Set(); arr.forEach(element => { set.add(element); }); // ๐๏ธ Set(3) { 'a', 'b', 'c' } console.log(set);
The function we passed to the Array.forEach() method gets called with each element in the array.
On each iteration, we use the set.add()
method to add the element to a new
Set
object.
The Set.add() method inserts a new element
with the supplied value into a Set
object, if the value is not already in the
Set
.
Set
constructor, but it is useful when you need to process the values before adding them to the Set
.You can also use the Array.reduce()
method to convert an array to a Set
object.
This is a three-step process:
Array.reduce()
method to iterate over the array.accumulator
variable to an empty Set
.Set
.const arr = ['a', 'a', 'b', 'c', 'c']; const set = arr.reduce((accumulator, current) => { accumulator.add(current); return accumulator; }, new Set()); // ๐๏ธ Set(3) { 'a', 'b', 'c' } console.log(set);
The function we passed to the Array.reduce() method gets called for each element in the array.
The initial value of the accumulator
variable is an empty Set
because that's
what we passed as the second argument to the Array.reduce()
method.
On each iteration, we use the Set.add()
method to add the current element to
the Set
object.
This is a three-step process:
Set
using the Set()
constructor.Set
and the array into a new Set
.Set
will contain the values of the original Set
and the array.const set1 = new Set(); const arr = ['bobby', 'hadz', 'com']; const newSet = new Set([...set1, ...arr]); console.log(newSet); // ๐๏ธ Set(3) { 'bobby', 'hadz', 'com' }
We created a new Set
using the Set()
constructor and used the spread syntax
(...) to unpack the values of the existing set and the array into the new Set
.
Set objects and arrays are iterable, so we are able to use the spread syntax
(...) to unpack them into a new Set
.
You can also use a for...of
loop to add an array of values to an existing
Set
.
for...of
loopThis is a two-step process:
for...of
loop to iterate over the array.Set.add()
method to add each array element to the Set
object.const set1 = new Set(); const arr = ['bobby', 'hadz', 'com']; for (const element of arr) { set1.add(element); } console.log(set1); // ๐๏ธ Set(3) { 'bobby', 'hadz', 'com' }
The for...of statement is
used to loop over iterable objects like arrays, strings, Map
, Set
and
NodeList
objects and generators
.
On each iteration, we use the Set.add()
method to add the current element to
the Set
object.
You can learn more about the related topics by checking out the following tutorials: