-
-
Save tizmagik/19ba6516064a046a37aff57c7c65c9cd to your computer and use it in GitHub Desktop.
// Since ES6 Map()'s' retain their order of insertion, it can sometimes be useful to get the last item or value inserted: | |
export const getLastItemInMap = map => Array.from(map)[map.size-1] | |
export const getLastKeyInMap = map => Array.from(map)[map.size-1][0] | |
export const getLastValueInMap = map => Array.from(map)[map.size-1][1] | |
// Obviously getLastKey and getLastValue can reuse getLastItem, but for maximum portability I opted for verbosity. |
Nice, thank you
a bit less wasteful (source)
export const lastItemInMap = map => Array.from(map).pop();
export const lastKeyInMap = map => Array.from(map.keys()).pop();
export const lastValueInMap = map => Array.from(map.values()).pop();
We can also spread them (benchmark)
export const lastItemInMap = map => [...map].pop();
export const lastKeyInMap = map => [...map.keys()].pop();
export const lastValueInMap = map => [...map.values()].pop();
export const lastItemInMap = map => [...map].pop();
thats slower by 20% .. https://jsbench.github.io/#7992a793df12b1ee16beaef7a9863c68
a bit less wasteful (source)
export const lastItemInMap = map => Array.from(map).pop(); export const lastKeyInMap = map => Array.from(map.keys()).pop(); export const lastValueInMap = map => Array.from(map.values()).pop();
It would remove the last element which may not always be required.
It would remove the last element
nope, cos 'pass by value'
var m = new Map(); m.set(1, 2); var lastValueInMap = map => Array.from(map.values()).pop();
console.dir({ lastval: lastValueInMap(m) }); console.dir({ map: m });
result
{ lastval: 2 }
{ map: Map(1) { 1 => 2 } }
Yes, got it, Array.from
is creating a shallow copy.
Any more space-efficient solutions?
There's this, although eslint doesn't like assignments in condition statements:
const getLastInMap = (iteratorFn) => (map) => {
const mapIterator = map[iteratorFn]();
let cur;
let last = undefined;
while (!(cur = mapIterator.next()).done) {
last = cur.value;
}
return last;
};
export const getLastItemInMap = getLastInMap('entries');
export const getLastValueInMap = getLastInMap('values');
export const getLastKeyInMap = getLastInMap('keys');
Any more space-efficient solutions?
There's this, although eslint doesn't like assignments in condition statements:
const getLastInMap = (iteratorFn) => (map) => { const mapIterator = map[iteratorFn](); let cur; let last = undefined; while (!(cur = mapIterator.next()).done) { last = cur.value; } return last; }; export const getLastItemInMap = getLastInMap('entries'); export const getLastValueInMap = getLastInMap('values'); export const getLastKeyInMap = getLastInMap('keys');
This is probably slower as it will need to iterate through the whole collection when you only need the last?
it will need to iterate through the whole collection when you only need the last?
do you have a better idea for javascript Map?
except hiding the map behind a proxy object, and tracking the set
and delete
calls
Nice 😝
Thank you so much!
const [lastKey, lastValue] = [...map].at(-1) || []
// Key only
const [lastKey] = [...map].at(-1) || []
// Value only
const [, lastValue] = [...map].at(-1) || []
// set
const lastValue = [...set].at(-1)
Nice! Thanks!