1

From JavaScript Iterators

var Iterator = function(arr){ return {
    index : -1,
    hasNext : function(){ return this.index <= arr.length; },
    hasPrevious: function(){ return this.index > 0; },

    current: function(){ return arr[ this["index"] ]; },

    next : function(){
        if(this.hasNext()){
            this.index = this.index + 1;            
            return this.current();
        } 
        return false;
    },

    previous : function(){
        if(this.hasPrevious()){
            this.index = this.index - 1
            return this.current();
        }
        return false;
    }
}   
};

var iter = Iterator([1,2,3]);

I want to rewrite this functionality through the addition of dynamic values

   added : function(data){
          arr.push(data);
          this.index++
        }


iter.added(1);
iter.added(6);
iter.added(7);
iter.added(8);

How do it ? I knows about iterator in ES 6 https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Iterator but i want support IE

6
  • Remove the this.index++ from added and it will work. Or doesn't it? Why not? Commented Feb 24, 2016 at 14:28
  • OK..... but i want when added then current index change Commented Feb 24, 2016 at 14:29
  • 1
    Ok, that's unexpected to me but go for it. Now what is your question? Commented Feb 24, 2016 at 14:30
  • Are you trying to implement next(value) similar to .send in python? Commented Feb 24, 2016 at 14:33
  • 1
    Btw, that hasNext method gives wrong results Commented Feb 24, 2016 at 14:47

1 Answer 1

1

You'd need to add the added() function similar to what you posted, then update the initial value of index so the next() and previous() functions work. You'd need to set it to the input array length so the next() function knows you're at the last element.

var Iterator = function(arr){ return {
    index : arr.length,
    hasNext : function(){ return this.index < arr.length - 1; },
    hasPrevious: function(){ return this.index > 0; },

    current: function(){ return arr[ this.index ]; },

    next : function(){
        if(this.hasNext()){
            this.index = this.index + 1;            
            return this.current();
        } 
        return false;
    },

    previous : function(){
        if(this.hasPrevious()){
            this.index = this.index - 1
            return this.current();
        }
        return false;
    },

    added: function(x){
        arr.push(x);
      this.index++;
    }
}   
};

var iter = Iterator([1,2,3]);
console.log(iter)
iter.added(1);
iter.added(6);
iter.added(7);
iter.added(8);
console.log(iter)
console.log(iter.next())
console.log(iter.previous())
console.log(iter.previous())
console.log(iter.previous())
console.log(iter.previous())
console.log(iter.next())
console.log(iter.current())

Which outputs:

Object {arr: Array[3], index: 3}
Object {arr: Array[7], index: 7}
false
8
7
6
1
6
6

Here it is in a fiddle: https://jsfiddle.net/8ojcrnkn/5/

Hope that helps!

Sign up to request clarification or add additional context in comments.

13 Comments

What exactly did you change in the code? What do you mean by "declare arr", it already is declared?
You need to be able to access arr from the added function, so you'd need to add it, see line 2.
When I call iter.next() return 6 .... but I want then return false /// only support prev()
@AdamKonieska: Notice that it's also in the hasNext and current functions. And nowhere it is accessed as a property. You don't need that. You can directly access the variable. See How do JavaScript closures work?
@zloctb: If you want the iterator to start at the end, you should make that index: arr.length
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.