admin管理员组文章数量:1431720
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
3 Answers
Reset to default 3You 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.
本文标签: javascriptIs there a better way to find if an iterator has reached its last valueStack Overflow
版权声明:本文标题:javascript - Is there a better way to find if an iterator has reached its last value? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745589280a2665100.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论