I found some answers suggesting iterating through the Map, setting the iterated element to a variable and then reading it after the iteration. But isn’t there any better, more elegant view? I could not find a better solution so far.
>Solution :
You can convert the Map to an array of entries, then get the last element.
const map = new Map([['a', 1], ['b', 2], ['c', 3]]);
let lastEntry = [...map].at(-1);
console.log(lastEntry);
// or only get last key/value
let lastKey = [...map.keys()].at(-1);
let lastValue = [...map.values()].at(-1);
console.log(lastKey, lastValue);
However, it’s more efficient to just iterate over the entries and keep the last one.
const map = new Map([['a', 1], ['b', 2], ['c', 3]]);
let entry; for (entry of map);
console.log(entry);