Set
可能使用Map
实现,它们也不是相同的数据结构。Map
的定义是正确的,但是Set
是一个包含唯一值的集合,不像数组可以有重复项。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);
Set
Map
。您将键映射到值。Set
示例:与来自不同组织的人进行会议。 一些人来自同一个组织。 我们需要列出所有不同的组织。 为此,我们可以使用Set,因为我们只想包含每个组织一次:
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);
}
Map
:我们有一群狗,想要为每只狗分配年龄。我们希望将每只狗的唯一名称映射到狗的年龄:
const dogs = new Map([['fluffy', 10], ['barkie', 13]]);
dogs.forEach((value, key) => console.log(key, value));
Map
与Object
有何不同?
Object
也是一个键值对集合,可以完成与Map
相同的任务(即创建键值对)。但是,Map
和Object
之间存在一些关键区别:
Map
内置Iterable,这使得它可以使用for of
循环或其实现的forEach()
方法,而普通JSObject
无法使用。Map
在其prototype
上具有一些不错的内置方法,可以让您很好地使用它。因为所有Objects
都继承自Object.prototype
,所以它可以访问更多有用的方法。例如,Map
上的size()
方法返回Map
中键的数量。var obj = {};
obj.name= "Anand Deep Singh";
console.log(obj.name); //logs "Anand Deep Singh"
同样地,在 ES6 中,我们可以使用普通对象。
var map = new Map();
map.set("name","Anand Deep Singh");
console.log(map.get("name")); //logs "Anand Deep Singh"
但值得注意的是,Map并不是使用字面对象语法创建的,而是使用set和get方法来存储和访问数据。
它有一个has方法来检查对象中是否存在该键,delete方法用于删除对象,clear方法用于清空整个对象。
Set是一组唯一的值。它只是一个唯一的列表。
var set = new Set(["a", "a","e", "b", "c", "b", "b", "b", "d"]);
console.log(set); //logs Set {"a", "e", "b", "c", "d"}
Set无法像数组一样被访问,它提供了与Map相同的方法。
现在主要有两种数据结构:
但这还不够适用于实际生活中的需求。这就是为什么Map和Set也存在的原因。
例如:
let map = new Map();
map.set('1', 'str1'); // a string key
map.set(1, 'num1'); // a numeric key
map.set(true, 'bool1'); // a boolean key
let hamid = { name: "Hamid" };
// hamid is the key for the map
map.set(hamid, 123); // an object key
示例:
let set = new Set();
let hamid= { name: "Hamid" };
let pete = { name: "Pete" };
let mary = { name: "Mary" };
// visits, some users come multiple times
set.add(hamid);
set.add(pete);
set.add(mary);
set.add(hamid);
set.add(mary);
// set keeps only unique values
alert( set.size ); // 3
const colorsSet = new Set([
'orange',
'orange',
'purple',
'pink',
'pink',
'pink',
]);
// here we have 2 oranges, 3 pinks and 1 purple, but since SETS can NOT have duplicates we will get:
// set(3) {'orange', 'purple', 'pink'})
console.log(colorsSet); // this can be proven also by looking at the size of a set
// We can get the size of a set:
console.log(colorsSet.size); // 3
// We can check if a certain element is in a set:
console.log(colorsSet.has('pink')); // If pink is in set, TRUE
console.log(colorsSet.has('white')); // If white is NOT in set, FALSE
// We can add new elements to a set:
colorsSet.add('black');
// We can also delete elements:
colorsSet.delete('purple'); // it must be straight forward names
console.log(colorsSet); // Set(3) {'orange', 'pink', 'black'}
// We can delete all of the elements in the set:
// colorsSet.clear();
//Sets are iterables, so we can loop over them:
for (const color of colorsSet) console.log(color);
const map = new Map(); // we create an empty map
// to fill up the map we can use SETS
map.set('nameOfFriend', 'locationOfFriend');
map.set('Alicia', 'NYC')
console.log(map.set('Alex", "Washington')); // if you call the set method, it will return the map.
// Map(3) {'nameOfFriend' => 'locationOfFriend', 'Alicia' => 'NYC', 'Alex' => 'Washington'}