Javascript 如何在使用next()时清除Iterable对象的当前位置?
在for循环之后或使用Javascript 如何在使用next()时清除Iterable对象的当前位置?,javascript,ecmascript-6,Javascript,Ecmascript 6,在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()
方法之后,如何清除当前位置
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个值
如何做到这一点
此示例来自本身无法“清除当前位置”。但是,在这个答案中,建议了一些有用的替代方法:.你所说的“清除当前位置”是什么意思?你想无限获取值吗?迭代器.return不是一个函数