最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a better way to find if an iterator has reached its last value? - Stack Overflow

programmeradmin3浏览0评论

I can do this:

let iter = mySet.values();
var val = iter.next();
var next = iter.next();
while(val.value){
  if(!next.done){
    // do something that I want to do to the last element
  }
  val = next;
  next  = iter.next();
}

but the 'next' variable seems clumsy. Is there a better way?

When I inspect the mySet object, I see a key of [[Entries]] with an array of values, but how would I access that?

I can do this:

let iter = mySet.values();
var val = iter.next();
var next = iter.next();
while(val.value){
  if(!next.done){
    // do something that I want to do to the last element
  }
  val = next;
  next  = iter.next();
}

but the 'next' variable seems clumsy. Is there a better way?

When I inspect the mySet object, I see a key of [[Entries]] with an array of values, but how would I access that?

Share Improve this question edited Apr 13, 2019 at 2:39 Frazer Kirkman asked Apr 12, 2019 at 22:55 Frazer KirkmanFrazer Kirkman 1,1541 gold badge15 silver badges24 bronze badges 9
  • 1 You can use a for..of loop if you do not need to know the index – jro Commented Apr 12, 2019 at 22:57
  • Does javascript offer the iterator.hasNext() method? Never used it in javascript, but that method is offered in other languages. – Taplar Commented Apr 12, 2019 at 23:02
  • 1 mySet.value(); Is that a Set? If so, do you mean .values()? – CertainPerformance Commented Apr 12, 2019 at 23:02
  • 1 @zfrisch why is a Set a performance hit? Set.has() is more performant than Array.find or Array.some or indexOf – charlietfl Commented Apr 12, 2019 at 23:49
  • 1 @zfrisch just did a quick search and found a valid jsperf jsperf./array-indexof-vs-set-has – charlietfl Commented Apr 13, 2019 at 0:17
 |  Show 4 more ments

3 Answers 3

Reset to default 3

You could use while(!next.done) and you only want to use next() once per iteration.

To figure out if it is the last value, store value in variable then reassign next and then check done before processing the value

function* myGen(arr) {
  let i = -1;
  while (++i < arr.length) {
    yield arr[i] * 2;
  }
}

const it = myGen([1, 2, 3]);

let next = it.next();

while (!next.done) {
  const val = next.value;
  next = it.next();
  console.log('value:', val, ' is last = ', next.done);
}

console.log('done:', next.done)

Not sure about others, but I see the last return value from the generator is missing when I use accepted solution by @charlietfl as the value after last .next() is ignored.

So, I used below simple do...while loop. Again just one call to .next()

function* numberGen() {
  yield 1;
  yield 2;
  yield 3;
  yield 4;
  yield 5;
  return 6;
}
console.clear();

const numbers = numberGen();

let nextNumber;
do {
  nextNumber = numbers.next();
  const val = nextNumber.value;
  console.log('value:', val, ' is last = ', nextNumber.done);
} while (!nextNumber.done);


console.log('done:', nextNumber.done);

From Mozilla's Doc:

"...next() method which returns an object with two properties: value, the next value in the sequence; and done, which is true if the last value in the sequence has already been consumed."

Nothing is wrong with your approach.

发布评论

评论列表(0)

  1. 暂无评论