What is Map?
Map is a data strcture that we can use to map values to keys, so just like an object data is stored in key value pairs in maps.
const programming = new Map();
We can also chain set method like below:
programming .set('tags', ['js', 'jquery', 'typescript']) .set('version', 'es6')
We can access values using keys:
Check value exist or not
programming.has('categories'); // true
Remove all the elementes
Difference between Maps and Objects:
- A Map is similar to Object, but any value can be used as key in maps, but keys in objects are only Strings and Symbols
- The key-value pairs in Map maintains the insertion order, whereas Object don’t.
- You can get the size of the Map, but we don’t have a built-in method to get size in Object
- Maps are iterable, whereas an Object is not iterable by-default.
- Maps have additional methods not available to normal object