Map object in Javascript. An alternate for plain javascript object with more flexiblilty
Автор: TK ℙ𝕣𝕠𝕘𝕣𝕒𝕞𝕞𝕚𝕟𝕘 Tips
Загружено: 2025-07-23
Просмотров: 52
Описание:
The Map object in JavaScript is a built-in data structure for key-value pairs, offering several advantages over plain objects, with its methods explained in the video.
You can initialize a Map with values by passing an array of [key, value] pairs to the constructor
Common Map Methods
Here are some of the most frequently used methods for interacting with Map objects:
map.set(key, value): Adds or updates a key-value pair in the Map. Returns the Map object itself, so you can chain set() calls.
map.get(key): Returns the value associated with the specified key. If the key is not found, it returns undefined.
map.has(key): Returns a boolean indicating whether a key exists in the Map.
map.delete(key): Removes a key-value pair from the Map. Returns true if an element was successfully removed, and false otherwise.
map.clear(): Removes all key-value pairs from the Map.
map.size: A property that returns the number of key-value pairs in the Map.
Iterating Over a Map
You can iterate over Map entries using various methods:
map.forEach(callbackFn): Executes a provided function once for each key-value pair in the Map.
map.keys(): Returns a new Iterator object that contains the keys for each element in the Map in insertion order.
map.values(): Returns a new Iterator object that contains the values for each element in the Map in insertion order.
map.entries(): Returns a new Iterator object that contains an [key, value] array for each element in the Map in insertion order.
You can also directly iterate over a Map without calling .entries() because Map objects are iterable by default, yielding [key, value] pairs:
Why use Map over Object?
While plain JavaScript objects can also store key-value pairs, Map offers several advantages:
Any data type as keys: Map allows you to use any data type (objects, functions, numbers, booleans, etc.) as keys, whereas plain objects only allow strings or Symbols as keys (non-string keys are converted to strings).
Order of insertion: Map maintains the order of key-value pairs as they were inserted, which is not guaranteed for plain objects (though modern engines generally preserve insertion order for string keys).
size property: Map has a direct size property for getting the number of elements, while with objects, you'd typically need to use Object.keys().length.
Better performance for frequent additions/removals: For scenarios involving frequent additions and removals of key-value pairs, Map generally offers better performance.
No prototype pollution: Plain objects have a prototype chain, which can lead to issues like prototype pollution or unexpected behavior if you're not careful with property names (e.g., if a key conflicts with a built-in Object.prototype property). Map objects do not have this issue.
In summary, Map is a powerful and versatile data structure in JavaScript that should be preferred over plain objects when you need a collection of key-value pairs with specific requirements like using non-string keys, maintaining insertion order, or easily getting the size of the collection.
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: