Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 737963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:46:56+00:00 2026-05-14T07:46:56+00:00

Do you know a JavaScript library that implements a generic Iterator class for collections

  • 0

Do you know a JavaScript library that implements a generic Iterator class for collections (be it Arrays or some abstract Enumerable) with a full set of features, like the Google Common or the Apache Commons?

Edit: Enumerable#each is not an Iterator class. I’m looking for an Iterator, something that would let us write something like:

var iterator = new Iterator(myCollection);
for (var element = iterator.next(); iterator.hasNext(); element = iterator.next()) {
    // iterator 
}

Edit : mamoo reminded us of the Iterator implementation in Mozilla’s Javascript 1.7. So the goal now is to find an implementation of this Iterator function in Javascript 1.5 (ECMA 4).

Edit2 : Why using an iterator when libraries (and ECMA 5) provide a each method? First, because each usually messes with this because the callback is call -ed (that’s why each accepts a second argument in Prototype). Then, because people are much more familiar with the for(;;) construct than with the .each(callback) construct (at least, in my field). Lastly, because an iterator can iterate over plain objects (see JavaScript 1.7).

Edit3 : I accepted npup’s anwser, but here is my shot at it :

function Iterator(o, keysOnly) {
    if (!(this instanceof arguments.callee))
      return new arguments.callee(o, keysOnly);
    var index = 0, keys = [];
    if (!o || typeof o != "object") return;
    if ('splice' in o && 'join' in o) {
        while(keys.length < o.length) keys.push(keys.length);
    } else {
        for (p in o) if (o.hasOwnProperty(p)) keys.push(p);
    }
    this.next = function next() {
        if (index < keys.length) {
            var key = keys[index++];
            return keysOnly ? key : [key, o[key]];
        } else throw { name: "StopIteration" };
    };
    this.hasNext = function hasNext() {
        return index < keys.length;
    };
}



var lang = { name: 'JavaScript', birthYear: 1995 };  
var it = Iterator(lang);
while (it.hasNext()) {
    alert(it.next());
}
//alert(it.next()); // A StopIteration exception is thrown  


var langs = ['JavaScript', 'Python', 'C++'];  
var it = Iterator(langs);
while (it.hasNext()) {
    alert(it.next());
}
//alert(it.next()); // A StopIteration exception is thrown  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:46:56+00:00Added an answer on May 14, 2026 at 7:46 am

    Ok, the enumerable pattern is not a real iterator then.

    Is this (below) useful for you? It conforms to the sematics you gave at least. As usual there are tradeoffs to be made here and there, and I didn’t think very hard when deciding this time :).
    And maybe you would like to be able to send in a number or two and iterate over a range in that way. But this could maybe be a start (there’s support for iterating over hashes, arrays and strings).

    It’s a whole demo page which runs itself and does some debug output, but the (possibly) interesting stuff is in the

    window.npup = (function() {
        [...]
    })();
    

    spot.

    Maybe it is just me who doesn’t get it at all, but what would you use such a java-like Iterator for in a real situation?

    Best
    /npup

    <html>
    <head>
    <title>untitled</title>
    </head>
    
    <body>
        <ul id="output"></ul>
    
    
    <script type="text/javascript">
    window.log = (function (outputAreaId) {
        var myConsole = document.getElementById(outputAreaId);
        function createElem(color) {
            var elem = document.createElement('li');
            elem.style.color = color;
            return elem;
        }
        function appendElem(elem) {
            myConsole.appendChild(elem);
        }
        function debug(msg) {
            var elem = createElem('#888');
            elem.innerHTML = msg;
            appendElem(elem);
        }
        function error(msg) {
            var elem = createElem('#f88');
            elem.innerHTML = msg;
            appendElem(elem);
        }
        return {
            debug: debug
            , error: error
        };
    })('output');
    
    
    window.npup = (function () {
        // Array check as proposed by Mr. Crockford
        function isArray(candidate) {
            return candidate &&
                typeof candidate==='object' &&
                typeof candidate.length === 'number' &&
                typeof candidate.splice === 'function' &&
                !(candidate.propertyIsEnumerable('length'));
        }
        function dontIterate(collection) {
            // put some checks chere for stuff that isn't iterable (yet)
            return (!collection || typeof collection==='number' || typeof collection==='boolean');
        }
        function Iterator(collection) {
            if (typeof collection==='string') {collection = collection.split('');}
            if (dontIterate(collection)) {throw new Error('Oh you nasty man, I won\'t iterate over that ('+collection+')!');}
            var arr = isArray(collection);
            var idx = 0, top=0;
            var keys = [], prop;
            if (arr) {top = collection.length;}
            else {for (prop in collection) {keys.push(prop);}}
            this.next = function () {
                if (!this.hasNext()) {throw new Error('Oh you nasty man. I have no more elements.');}
                var elem = arr ? collection[idx] : {key:keys[idx], value:collection[keys[idx]]};
                ++idx;
                return elem;
            };
            this.hasNext = function () {return arr ? idx<=top : idx<=keys.length;};
        }
        return {Iterator: Iterator};
    })();
    
    var element;
    
    log.debug('--- Hash demo');
    var o = {foo:1, bar:2, baz:3, bork:4, hepp: {a:1,b:2,c:3}, bluff:666, bluff2:777};
    var iterator = new npup.Iterator(o);
    for (element = iterator.next(); iterator.hasNext(); element = iterator.next()) {
        log.debug('got elem from hash: '+element.key+' => '+element.value);
        if (typeof element.value==='object') {
            var i2 = new npup.Iterator(element.value);
            for (var e2=i2.next(); i2.hasNext(); e2=i2.next()) {
                log.debug('&nbsp;&nbsp;&nbsp;&nbsp;# from inner hash: '+e2.key+' => '+e2.value);
            }
        }
    }
    log.debug('--- Array demo');
    var a = [1,2,3,42,666,777];
    iterator = new npup.Iterator(a);
    for (element = iterator.next(); iterator.hasNext(); element = iterator.next()) {
        log.debug('got elem from array: '+ element);
    }
    log.debug('--- String demo');
    var s = 'First the pants, THEN the shoes!';
    iterator = new npup.Iterator(s);
    for (element = iterator.next(); iterator.hasNext(); element = iterator.next()) {
        log.debug('got elem from string: '+ element);
    }
    log.debug('--- Emptiness demo');
    try {
        log.debug('Try to get next..');
        var boogie = iterator.next();
    }
    catch(e) {
        log.error('OW: '+e);
    }
    
    log.debug('--- Non iterables demo');
    try{iterator = new npup.Iterator(true);} catch(e) {log.error('iterate over boolean: '+e);}
    try{iterator = new npup.Iterator(6);} catch(e) {log.error('iterate over number: '+e);}
    try{iterator = new npup.Iterator(null);} catch(e) {log.error('iterate over null: '+e);}
    try{iterator = new npup.Iterator();} catch(e) {log.error('iterate over undefined: '+e);}
    
    </script>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.