containersecmascript-6

How to get a last element of ES6 Map without iterations?


How to get a last element of ES6 Map/Set without iterations(forEach or for of) pass through a full length of the container?


Solution

  • Maps are iterable and ordered, but they are not arrays so they don't have any indexed access. Iterating to the final element is the best you can do. The simplest case being

     Array.from(map.values()).pop();