JS: Map Object Tutorial
New in JS2015.
What is Map Object
- Map object is a collections 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 Arbitrary 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 arbitrary properties to a map object xx.pp = 22; console.log(xx.pp === 22);
JavaScript, Map Object
- JS: Map Object 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: Map Equality π
- 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 Object
- JS: Map Constructor
- JS: Map.prototype