Skip to content

Instantly share code, notes, and snippets.

@atomize
Created August 28, 2018 20:50
Show Gist options
  • Save atomize/bc33d18806bf46eedfffb694283eeffe to your computer and use it in GitHub Desktop.
Save atomize/bc33d18806bf46eedfffb694283eeffe to your computer and use it in GitHub Desktop.
The Map object holds key-value pairs. Any value (both objects and primitive values) may be used as either a key or a value. This is the relation of Map() with Array objects
var kvArray = [['key1', 'value1'], ['key2', 'value2']];
// Use the regular Map constructor to transform a 2D key-value Array into a map
var myMap = new Map(kvArray);
myMap.get('key1'); // returns "value1"
// Use the Array.from function to transform a map into a 2D key-value Array
console.log(Array.from(myMap)); // Will show you exactly the same Array as kvArray
// Or use the keys or values iterators and convert them to an array
console.log(Array.from(myMap.keys())); // Will show ["key1", "key2"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment