Home > Web Front-end > Front-end Q&A > What is the difference between map and object in es6

What is the difference between map and object in es6

青灯夜游
Release: 2022-11-17 13:56:23
Original
3914 people have browsed it

Difference: 1. The key of Map can be any value, while the key of Object must be a String or Symbol. 2. The keys in Map are ordered, while the keys in Object are unordered. 3. The number of key-value pairs of Map can be easily obtained through the size attribute, while the number of key-value pairs of Object can only be calculated manually. 4. Map can be iterated directly, but Object cannot be iterated directly. 5. Map performs better in scenarios where key-value pairs are frequently added or deleted, while Object is less efficient.

What is the difference between map and object in es6

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

The concept of Map and Object

Object

In ECMAScript, Object is a special object. It is a top-level object in itself and also a constructor, through which (such as: new Object()) you can create an object. We can think that all objects in JavaScript are an instance of Object. Objects can be declared using the literal method const obj = {}

Map

Map It is a subclass of Object, which can store any type of data in an orderly manner, using key-value pairs to store, where the keys can store any type. A map instance can be obtained through const m = new Map();.

Map is similar to an object and is also a collection of key-value pairs, but the scope of "key" is not limited to strings. Various types of values ​​(including objects) can be used as keys. In other words, the Object structure provides "string-value" correspondence, and the Map structure provides "value-value" correspondence, which is a more complete implementation of the Hash structure. If you need a "key-value" data structure, Map is more suitable than Object. Map can accept an array as parameter. The members of this array are arrays representing key-value pairs.

The difference between Map and Object

##MapObjectType of keyThe key of a Map can be any value, including function, object or any basic type. The key of an Object must be a String or Symbol. The order of keysThe keys in Map are ordered. Therefore, when iterating, a Map object returns key values ​​in insertion order. The keys of an Object are unordered. Note: Since the ECMAScript 2015 specification, objects do preserve the creation order of string and symbol keys; therefore, iterating over an object with only string keys will produce keys in insertion order. SizeThe number of key-value pairs in Map can be easily obtained through the size attribute The number of key-value pairs in Object can only be calculated manually IterationMap is iterable, so it can be iterated directly. Iterating over an Object requires obtaining its key in some way before it can be iterated. PerformancePerforms better in scenarios where key-value pairs are frequently added or deleted. Not optimized in scenarios where key-value pairs are frequently added and deleted.

[Recommended learning:

javascript advanced tutorial]

The above is the detailed content of What is the difference between map and object in es6. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
es6
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template