JS: Map Tutorial
(new in JS: ECMAScript 2015)
What is Map Object
- Map object is a ordered list of key-value pairs. Each pair is called a entry.
- Key can be any Value Types.
- Value can be any value types.
- Keys are always distinct.
- The insertion order of entries are maintained.
The map object is designed to be used as a map data structure (similar to Python Dictionary or Ruby Hash Table or Java Map. ).
Create Map
new Map()
- Create a empty map.
new Map(iterable)
- Create a map from values in iterable object iterable.
〔see Iterable Object〕
const xx = new Map(); // add new item xx.set(1, "n1"); xx.set(2, "n2"); console.log(xx); // Map { 1 => "n1", 2 => "n2" }
Parent of Map Objects
The parent of any map object is Map.prototype
.
Add, Modify Entry
Delete Entry
Check Key Existence
Get Key's Value
Size of Map
Clear Map
Iterate Map
Get All Keys
Get All Values
Add Properties to Map
Because map is a object, you can add object properties to it, but you shouldn't do it.
const xx = new Map([[3, "n3"], [4, "n4"], [5, "n5"]]); // you can add properties to a map object xx.pp = 22; console.log(xx.pp === 22);
JavaScript, Map Object
- JS: Map Tutorial
- JS: Difference, Object vs Map
- JS: Literal Expression for Map
- JS: Nested Map
- JS: Iterate Map Object
- JS: Convert Object to Map 💠
- JS: Convert Map to Object 💠
- JS: Test Equality of Map Objects 💠
- JS: Map Filter 💠
- JS: Swap Key Value of Map 💠
- JS: Sort Map 💠
- JS: Truncate Map 💠
- JS: Map Substract 💠
- JS: How Map Determines Uniqueness of Keys
- JS: Map (class)
- JS: Map Constructor
- JS: Map.prototype