javascript Map 对象与 Set 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24085708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript Map object vs Set object
提问by nznoor
回答by Daniel A. White
Provided you are talking about the ES6 types, they aren't the same data structure even though the Set
might be implemented with a Map
.
假设您在谈论 ES6 类型,它们不是相同的数据结构,即使Set
可能使用Map
.
Your definition of Map
is right, but a Set
is a collection of unique values, unlike an array which can have duplicates.
您的定义Map
是正确的,但 aSet
是唯一值的集合,与可以有重复的数组不同。
var array = [1, 2, 3, 3];
var set = new Set(array); // Will have [1, 2, 3]
assert(set.size, 3);
var map = new Map();
map.set('a', 1);
map.set('b', 2);
map.set('c', 3);
map.set('C', 3);
map.set('a', 4); // Has: a, 4; b, 2; c: 3, C: 3
assert(map.size, 4);
回答by Anand Deep Singh
Developers usually just use regular JavaScript objects when they want maps.
开发人员通常只在需要地图时使用常规 JavaScript 对象。
var obj = {};
obj.name= "Anand Deep Singh";
console.log(obj.name); //logs "Anand Deep Singh"
similarly in ES6, we can use regular object.
同样在ES6 中,我们可以使用常规对象。
var map = new Map();
map.set("name","Anand Deep Singh");
console.log(map.get("name")); //logs "Anand Deep Singh"
But noticeable thing is a Mapisn't created with the literal object syntax, and that one uses setand getmethods to storeand access data.
但值得注意的是Map不是用文字对象语法创建的,而是使用set和get方法来存储和访问数据。
It has a hasmethod to check whether the key exists in the object or not, deletemethod to delete the object and clearmethod to clear the entire object.
它有一个has方法来检查对象中是否存在key,delete方法删除对象,clear方法清除整个对象。
Setis a unique list of values. It's simply a unique list.
Set是一个唯一的值列表。这只是一个独特的列表。
var set = new Set(["a", "a","e", "b", "c", "b", "b", "b", "d"]);
console.log(set); //logs Set {"a", "e", "b", "c", "d"}
A Set can't be accessed like an array, and it provides the same methods as a Map.
Set 不能像数组一样访问,它提供与 Map 相同的方法。
回答by Willem van der Veen
Summary:
概括:
- Use a
Set
when your dataset needs to be composed of unique values - Use a
Map
when you have pairsof associated data. You mapthe keys to the values
Set
当您的数据集需要由唯一值组成时使用Map
当您有成对的关联数据时,请使用 a 。您将键映射到值
Example Set
:
示例Set
:
There is a meeting with people coming from different organizations. Some people come from the same organization. We need to compose a list all the different organzations. For this we can use a set since we only want to include every organization once:
有来自不同组织的人的会议。有些人来自同一个组织。我们需要列出所有不同的组织。为此,我们可以使用一个集合,因为我们只想包含每个组织一次:
const organization = new Set();
organization.add('org1');
organization.add('org2');
organization.add('org3');
organization.add('org1');
organization.add('org3');
organization.add('org1');
for(let org of organization){
console.log(org);
}
Example Map
:
示例Map
:
We have a pack of dogs and want to assign an age to each dog. We want to map the unique name of each dog to the age of the dog:
我们有一群狗,想给每只狗指定一个年龄。我们想将每只狗的唯一名称映射到狗的年龄:
const dogs = new Map([['fluffy', 10], ['barkie', 13]]);
dogs.forEach((value, key) => console.log(key, value));
How is Map
different from an Object
?
如何Map
从不同Object
?
An Object
is also a collection of key value pairs and can fulfill often the same purpose as a Map
can (which is creating key-value pairs). However, there are some key differences between a Map
and an Object
:
AnObject
也是键值对的集合,通常可以实现与 can 相同的目的Map
(即创建键值对)。但是, aMap
和 an之间存在一些主要区别Object
:
Map
is built in Iterable, this allows it to use thefor of
loop or its implementation of theforEach()
method which an plain JSObject
cannot use.Map
has some nice built in methods on itsprototype
which makes working with it very nicely. Because alObjects
inherit fromObject.prototype
is has access to more useful methods. For example, thesize()
method onMap
returns the number of keys in theMap
.
Map
内置于 Iterable 中,这允许它使用普通 JS无法使用的for of
循环或其forEach()
方法的实现Object
。Map
它有一些很好的内置方法prototype
,可以很好地使用它。因为 alObjects
继承自Object.prototype
is 可以访问更多有用的方法。例如,size()
方法 onMap
返回Map
.