Skip to content

Instantly share code, notes, and snippets.

@tizmagik
Created August 13, 2016 03:22
Show Gist options
  • Save tizmagik/19ba6516064a046a37aff57c7c65c9cd to your computer and use it in GitHub Desktop.
Save tizmagik/19ba6516064a046a37aff57c7c65c9cd to your computer and use it in GitHub Desktop.
ES6 Last Item in Map()
// 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.
@EngrEric
Copy link

EngrEric commented Jun 2, 2020

Nice, thank you

@milahu
Copy link

milahu commented Oct 10, 2020

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();

@cmlarsen
Copy link

cmlarsen commented Dec 14, 2020

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();

@milahu
Copy link

milahu commented Dec 14, 2020

export const lastItemInMap = map => [...map].pop();

thats slower by 20% .. https://jsbench.github.io/#7992a793df12b1ee16beaef7a9863c68

@salman-monetate
Copy link

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.

@milahu
Copy link

milahu commented Apr 1, 2021

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 } }

@salman-monetate
Copy link

Yes, got it, Array.from is creating a shallow copy.

@joshkarges
Copy link

joshkarges commented Apr 6, 2021

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');

@olajohn-ajiboye
Copy link

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?

@milahu
Copy link

milahu commented May 8, 2021

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

@Holybasil
Copy link

Nice 😝

@Binary-Bytes
Copy link

Thank you so much!

@faustienf
Copy link

faustienf commented Aug 1, 2022

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment