1

How can I clear the current position after the for loop or after I used .next() method ?

 const arr = [1,4,2];
 for(v of arr){
     // console.log(v); // 1 4 2
 }
 //  console.log(arr.next())  //arr.next is not a function

 /*
 * 
 * Why? because it is an iterable not an iterator,
 * and to be able to use .next we need to call the iterator method 
 * this iterable’s Symbol.iterator returns:
 * */
 const iter = arr[Symbol.iterator]();

 console.log(iter.next());
 console.log(iter.next());
 console.log(iter.next());
 console.log(iter.next());

After this I want get 1 value from item.

How to do this ?

This example is from http://blog.dubizzle.com/boilerroom/2017/01/09/introduction-javascript-iterables-iterators-generators/

GôTô
  • 7,974
  • 3
  • 32
  • 43
zloctb
  • 10,592
  • 8
  • 70
  • 89

1 Answers1

1

There's no way to 'clear the current position' per se. However, helpful alternatives were suggested in this answer: Is it possible to reset an ECMAScript 6 generator to its initial state?.

Community
  • 1
  • 1
Deividas
  • 6,437
  • 2
  • 26
  • 27