Set traversal in JavaScript
This post will discuss how to traverse a set in JavaScript.
There are several methods to traverse a set in JavaScript. A set is a collection of unique values of any type. Traversing a set means accessing each value in the set in a certain order. Here are some of the methods that we can use:
1. Using for…of loop
We can use the for…of loop to traverse the values of a set in the same order that they were added to the set. The for…of loop is a simple and concise way to loop over any iterable object, such as a set, and assigns each value to a variable in each iteration. For instance:
|
1 2 3 4 5 6 7 8 9 10 |
// Create a set const mySet = new Set([1, 2, 3]); // Iterate over the values using the for…of loop for (const value of mySet) { console.log(value); } // 1 // 2 // 3 |
2. Using Set.forEach() function
Another option is to use the Set.forEach() function to traverse the values of a set in the same order that they were added to the set. It takes a callback function as an argument and executes it for each value in the set. The callback function can take up to three parameters: the current value, the same value, and the set object. The second argument is provided for compatibility with Array.forEach(), but it is always the same as the first argument for sets. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 |
// Create a set const mySet = new Set([1, 2, 3]); // Iterate over the values using the forEach function mySet.forEach(value => { console.log(value); }); // 1 // 2 // 3 |
3. Using Set.values() function
A third option is to use the Set.values() function to get an iterator object that contains all the values in the set in insertion order. We can then use the iterator functions, such as next(), to get the next value from the iterator, or use a loop to traverse all the values. The Set.values() function is useful when we want to access or manipulate the values of the set without modifying the set itself. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Create a set const mySet = new Set([1, 2, 3]); // Get an iterator of values const myIterator = mySet.values(); // Iterate over all values using a while loop while (true) { // Get the next value from the iterator const {value, done} = myIterator.next(); // If done is true, break out of the loop if (done) { break; } console.log(value); } // 1 // 2 // 3 |
4. Using Array.map() function
The idea here is to convert a set into an array and then traverse the array using any array iteration function, such as a for loop or a Array.map() function. We can use the Array.from() function or the spread operator with array literal to get a new array with the same values. For example, we can use something like this:
|
1 2 3 4 5 6 7 8 9 |
let mySet = new Set([1, 2, 3]); [...mySet].map(function(value) { console.log(value); }); // 1 // 2 // 3 |
That’s all about traversing a set in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)