Created
August 28, 2018 20:50
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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