Iterators in JavaScript PRO

JavaScript allows us to use iterators for going over the properties of a specific object or for going over the elements of a specific array. The following code sample shows how simple it is to use iterators in JavaScript.

<!DOCTYPE html>
<html>
<head>
    <title>The Iterator Function</title>
</head>
<body>
    <script type="text/javascript">
        var student = { name: 'James', id: 234234123, average: 88.2 };
        var vec = [23,54,76,8,345,4];
        var ob1 = Iterator(vec);
        //iterating using the next function
        document.writeln("<br/>"+ob1.next());
        document.writeln("<br/>"+ob1.next());
        document.writeln("<br/>"+ob1.next());
        document.writeln("<br/>");
        //iterating using for..in loop
        var ob2 = Iterator(vec);
        for(var item in ob2)
        {
            document.writeln("<br/>"+item);
        }
    </script>
</body>
</html>

The following video clip goes over this code sample, shows its execution and explains each and every part of it.

When calling next() once reaching to the last key value pair the StopIteration exception will be thrown. The following code sample shows that.

<!DOCTYPE html>
<html>
<head>
    <title>StopIteration Exception Demo</title>
</head>
<body>
<script type="text/javascript">
    try
    {
        var student = { name: 'James', id: 234234123, average: 88.2 };
        var ob = Iterator(student);
        document.writeln("<br/>"+ob.next());
        document.writeln("<br/>"+ob.next());
        document.writeln("<br/>"+ob.next());
        document.writeln("<br/>"+ob.next());
        document.writeln("<br/><br/>worked fine!");
    }
    catch(ex)
    {
        document.writeln("<br/><br/>"+ex);
    }
</script>
</body>
</html>

The following video clip goes over this code sample, shows its execution and explains each and every part of it.

In order to iterate the keys only we should pass over the value true as a second argument to the Iterator function. The following code sample shows that.

<!DOCTYPE html>
<html>
<head>
    <title>Iterating The Keys</title>
</head>
<body>
<script type="text/javascript">
    var student = { name: 'James', id: 234234123, average: 88.2 };
    var ob = Iterator(student,true);
    document.writeln("<br/>"+ob.next());
    document.writeln("<br/>"+ob.next());
    document.writeln("<br/>"+ob.next());
</script>
</body>
</html>

The following video clip goes over this code sample, shows its execution and explains each and every part of it.

You can find more video clips, code samples and various other training material for learning JavaScript in my free online courses website at http://abelski.lifemichael.com.

Share:

The Visitor Design Pattern

The Visitor Design Pattern

The visitor design pattern allows us to add operations to objects that already exist without modifying their classes and without extending them.

What are Anti Patterns?

Anti Patterns

Unlike design patterns, anti patterns just seem to be a solution. However, they are not a solution and they cause additional costs.

Virtual Threads in Java Professional Seminar

Virtual Threads in Java

The use of virtual threads can assist us with improving the performance of our code. Learn how to use virtual threads effectively.

NoSQL Databases Courses, Seminars, Consulting, and Development

MongoDB Design Patterns Meetup

The use of MongoDB involves with various cases in which we can overcome performance issues by implementing specific design patterns.

image of woman and database

Record Classes in Java

Learn how to define record classes in Java, and when to use record classes in your code. Stay up to date with the new Java features.

Accessibility | Career | Conferences | Design Patterns | JavaScript | Meetups | PHP | Podcasts | Python | Self Learning

Teaching Methodologies | Fullstack | C++ | C# | CSS | Node.js | Angular | Java | Go | Android | Kotlin | Swift | Academy

Front End Development | Scala | Architectures | Cloud | Big Data | Internet of Things | Kids Learn Programming

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Skip to content Update cookies preferences