Created
August 7, 2015 19:30
-
-
Save bendc/a978918e26970af513e9 to your computer and use it in GitHub Desktop.
Objects and maps compared
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
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