Skip to content

Instantly share code, notes, and snippets.

@bendc
Created August 7, 2015 19:30
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bendc/a978918e26970af513e9 to your computer and use it in GitHub Desktop.
Save bendc/a978918e26970af513e9 to your computer and use it in GitHub Desktop.
Objects and maps compared
const arr = [["foo", "bar"], ["hello", "world"]];
// Create object from array of tuples
const obj = {}; arr.forEach(el => obj[el[0]] = el[1]);
const map = new Map(arr);
// Get object size
const objSize = Object.keys(obj).length;
const mapSize = map.size;
// Get array of values
const objValues = Object.keys(obj).map(key => obj[key]);
const mapValues = [...map.values()];
// Clear object
Object.keys(obj).forEach(key => delete obj[key]);
map.clear();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment