المعرفة:: JavaScript
الحالة::مؤرشفة
المراجع:: The Complete JavaScript Course 2022 From Zero to Expert
-
A data structure that can be used to map values to keys, like objects.
-
In maps, the keys can be any type such as arrays, booleans, numbers etc. while in objects keys are strings.
Creating Maps
Methods
Set
Get
Has
Size
Delete
Clear
Using Arrays as Keys
- In order to use arrays, the set and get keys must be the same object. Because arrays are Reference Type.
Using DOM element as Key
Converting Objects to Maps
Converting Map to Array
Iterating Maps
Maps iteration doesn’t require using entries()
method.
وصلة للملاحظة الرئيسة
Objects vs Maps
Objects | Maps |
---|
More “traditional” key/value store (“abused” objects) | Better performance Keys can have any data type |
Easier to write and access values with . and [] | Easy to iterate and to compute size |
Use when you need to include functions (methods) | Use when you simply need to map key to values |
Use when working with JSON (can convert to map) | Use when you need keys that are not strings |