如何在不迭代(forEach 或 for of)遍历整个容器的情况下获取 ES6 Map/Set 的最后一个元素?
映射是可迭代且有序的,但它们不是数组,因此它们没有任何索引访问。迭代到最终元素是您能做的最好的事情。最简单的情况是
Array.from(map.values()).pop();
const getLastItemInMap = (map) => [...map][map.size-1];
const getLastKeyInMap = (map) => [...map][map.size-1][0];
const getLastValueInMap = (map) => [...map][map.size-1][1];
const lastEvent = [...gameEvents.entries()].at(-1);
// gameEvents 是一张地图