
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Loop Through a Set Using JavaScript
In the set that we implemented, we can create a for each function in our class and accept a callback that we can call on every element. Let's see how we can implement such a function −
Example
forEach(callback) { for (let prop in this.container) { callback(prop); } }
You can test this using −
Example
const testSet = new MySet(); testSet.add(1); testSet.add(2); testSet.add(5); testSet.forEach(elem => console.log(`Element is ${elem}`));
Output
This will give the output −
Element is 1 Element is 2 Element is 5
The ES6 Set API also provides the same functionality using the forEach method.
Advertisements