JavaScript 中的地图与对象 [英] Map vs Object in JavaScript

查看:20
本文介绍了JavaScript 中的地图与对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现了此功能:

<块引用>

Map:Map 对象是简单的键/值映射.

这让我很困惑.常规 JavaScript 对象是字典,那么 地图 不同于字典?从概念上讲,它们是相同的(根据 关于堆栈溢出)

文档也无济于事:

<块引用>

Map 对象是键/值对的集合,其中键和值都可以是任意的 ECMAScript 语言值.不同的键值只能出现在 Map 集合中的一个键/值对中.使用创建 Map 时选择的比较算法区分不同的键值.

<块引用>

Map 对象可以按插入顺序迭代其元素.Map 对象必须使用哈希表或其他机制来实现,平均而言,这些机制提供的访问时间与集合中的元素数量是次线性的.此 Map 对象规范中使用的数据结构仅用于描述 Map 对象所需的可观察语义.它并不打算成为一个可行的实施模型.

……对我来说仍然像一个对象,很明显我错过了一些东西.

为什么 JavaScript 获得(得到很好的支持)Map 对象?它有什么作用?

解决方案

据 Mozilla:

<块引用>

Map 对象可以按插入顺序迭代其元素 - for..of 循环将为每次迭代返回一个 [key, value] 数组.

<块引用>

对象与地图类似,都可以让您将键设置为值,检索这些值,删除键,并检测是否有东西存储在一个键上.因此,对象已被用作地图历史上;但是,对象之间存在重要差异以及让使用地图变得更好的地图.

一个对象有一个原型,所以映射中有默认的键.但是,这可以使用 map = Object.create(null) 绕过.这对象的键是字符串,它们可以是 Map 的任何值.您可以轻松获取地图的大小,而您必须手动保留跟踪对象的大小.

地图

顺序迭代是开发人员长期以来一直想要的功能,部分原因是它确保在所有浏览器中具有相同的性能.所以对我来说这很重要.

myMap.has(key) 方法将特别方便,还有 myMap.size 属性.

I just discovered this feature:

Map: Map objects are simple key/value maps.

That confused me. Regular JavaScript objects are dictionaries, so how is a Map different from a dictionary? Conceptually, they're identical (according to another question on Stack Overflow)

The documentation doesn't help either:

Map objects are collections of key/value pairs where both the keys and values may be arbitrary ECMAScript language values. A distinct key value may only occur in one key/value pair within the Map’s collection. Distinct key values as discriminated using the a comparision algorithm that is selected when the Map is created.

A Map object can iterate its elements in insertion order. Map object must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection. The data structures used in this Map objects specification is only intended to describe the required observable semantics of Map objects. It is not intended to be a viable implementation model.

…still sounds like an object to me, so clearly I've missed something.

Why is JavaScript gaining a (well-supported) Map object? What does it do?

解决方案

According to Mozilla:

A Map object can iterate its elements in insertion order - a for..of loop will return an array of [key, value] for each iteration.

and

Objects are similar to Maps in that both let you set keys to values, retrieve those values, delete keys, and detect whether something is stored at a key. Because of this, Objects have been used as Maps historically; however, there are important differences between Objects and Maps that make using a Map better.

An Object has a prototype, so there are default keys in the map. However, this can be bypassed using map = Object.create(null). The keys of an Object are Strings, where they can be any value for a Map. You can get the size of a Map easily while you have to manually keep track of size for an Object.

Map

The iterability-in-order is a feature that has long been wanted by developers, in part because it ensures the same performance in all browsers. So to me that's a big one.

The myMap.has(key) method will be especially handy, and also the myMap.size property.

这篇关于JavaScript 中的地图与对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆