如何在 for 循环之后或使用 .next()
方法之后清除当前位置?
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());
此后我想从项目中获取 1 个值。
如何做到这一点?
最佳答案
本身无法“清除当前位置”。但是,此答案中建议了有用的替代方案:Is it possible to reset an ECMAScript 6 generator to its initial state? .
关于javascript - 使用 next() 时如何清除 Iterable 对象的当前位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42411078/