Maps

Added in ES 2015.
For key-value pairs that need to be updated frequently the best collection to use is Map.


Advantages over Objects

the key does not have to be a string
browsers are optimised for large maps, not large objects
maps preserve order


const myMap = new Map() 
myMap.set('prop1', 'value1')
myMap.set('prop2', 'value2')

console.log( myMap.get('prop1')

with chaining

const myMap = new Map() 
  .set('prop1', 'value1')
  .set('prop2', 'value2')

with an array

const myMap = new Map() 
  [
    ['prop1', 'value1'],
    ['prop2', 'value2']
]

removing

myMap.delete('prop1');  
myMap.clear(); // removing all

This does not return an array - this returns a MapIterator

myMap.keys()  // { prop1, prop2} 

Methods


MethodMutatesDescription
entries  
clear  
delete  
get  
has  
keys  
set  
sort  



© 2020 Better Solutions Limited. All Rights Reserved. © 2020 Better Solutions Limited TopPrevNext