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

I can do this:let iter = mySet.values();var val = iter.next();var next = iter.next();while(val.value

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745589280a4634734.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信