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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:10:07  来源:igfitidea点击:

javascript Map object vs Set object

javascript

提问by nznoor

JavaScript Mapand Setobjects are both iterable objects. Both store object by [key, value]pair. I want to know when to use what? Is there any preference one over another?

JavaScriptMapSet对象都是可迭代对象。两者都[key, value]成对存储对象。我想知道什么时候用什么?是否有任何偏好?

回答by Daniel A. White

Provided you are talking about the ES6 types, they aren't the same data structure even though the Setmight be implemented with a Map.

假设您在谈论 ES6 类型,它们不是相同的数据结构,即使Set可能使用Map.

Your definition of Mapis right, but a Setis 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不是用文字对象语法创建的,而是使用setget方法来存储访问数据

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 Setwhen your dataset needs to be composed of unique values
  • Use a Mapwhen 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 Mapdifferent from an Object?

如何Map从不同Object

An Objectis also a collection of key value pairs and can fulfill often the same purpose as a Mapcan (which is creating key-value pairs). However, there are some key differences between a Mapand an Object:

AnObject也是键值对的集合,通常可以实现与 can 相同的目的Map(即创建键值对)。但是, aMap和 an之间存在一些主要区别Object

  • Mapis built in Iterable, this allows it to use the for ofloop or its implementation of the forEach()method which an plain JS Objectcannot use.
  • Maphas some nice built in methods on its prototypewhich makes working with it very nicely. Because al Objectsinherit from Object.prototypeis has access to more useful methods. For example, the size()method on Mapreturns the number of keys in the Map.
  • Map内置于 Iterable 中,这允许它使用普通 JS无法使用的for of循环或其forEach()方法的实现Object
  • Map它有一些很好的内置方法prototype,可以很好地使用它。因为 alObjects继承自Object.prototypeis 可以访问更多有用的方法。例如,size()方法 onMap返回Map.