Skip to content

Instantly share code, notes, and snippets.

@naveenrawat51
Forked from tizmagik/getLastInMap.js
Created November 11, 2021 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naveenrawat51/4c19a5fbd6af717d20282040c61d8a37 to your computer and use it in GitHub Desktop.
Save naveenrawat51/4c19a5fbd6af717d20282040c61d8a37 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.
@naveenrawat51
Copy link
Author

get last from map object

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