如何在没有迭代的情况下获取ES6 Map/Set的最后一个元素(forEach或for for)通过容器的整个长度?
我有一个项目使用我想要移动到es6集或地图的对象数组.
我需要快速从它们中获取一个随机项(对于我当前的数组来说显然是微不足道的).我该怎么做?
如何获得生成器的第n个值?
function *index() {
let x = 0;
while(true)
yield x++;
}
// the 1st value
let a = index();
console.log(a.next().value); // 0
// the 3rd value
let b = index();
b.next();
b.next();
console.log(b.next().value); // 2
// the nth value?
let c = index();
let n = 10;
console.log(...); // 9
Run Code Online (Sandbox Code Playgroud)