DEV Community

Discussion on: 1 line of code: How to remove all duplicates from an Array

Collapse
 
michaelcurrin profile image
Michael Currin

I just learned of an alternative for set to array

const myArr = Array.from(mySet1)
Enter fullscreen mode Exit fullscreen mode

And if you want to iterate over a set without converting to an array.

for (const item of mySet1) { 
  console.log(item)
}
Enter fullscreen mode Exit fullscreen mode

developer.mozilla.org/en-US/docs/W...

Collapse
 
qm3ster profile image
Mihail Malo

Array.from goes through iterator protocol for Set and Map, so it's exactly equivalent to the [... sugar.