在过去的几年中,JavaScript 开发人员一直在关注 ECMAScript 标准的发展。前一个版本,即 ES9 为开发人员带来了很多实用的功能。如今,ECMAScript 的最新版本,即 ES10 提供了更多功能,特别是有关集合数据类型的增强功能。在本文中,我们将深入探讨 ES10 引入的 Set 新增功能。
Set 数据结构
Set 是一种用于存储唯一值的集合数据类型。即使添加相同的元素,它们也只会保留其一份。下面是一个简单的 Set 示例:
const mySet = new Set();
mySet.add('foo');
mySet.add('bar');
mySet.add('baz');
mySet.add('foo'); // 这个元素不会被添加进集合中
console.log(mySet); // 输出 Set(3) {"foo", "bar", "baz"}Set.prototype.size 属性
在 ES10 中,size 属性被添加到了 Set 原型中。它用于获取 Set 中元素的数量。
const mySet = new Set(['foo', 'bar', 'baz']); console.log(mySet.size); // 输出 3
Set.prototype.forEach() 方法
ES6 中的 Map 和 Array 有一个 forEach 方法用于迭代每个元素并执行相应的操作。ES10 引入了一个类似的 forEach 方法用于 Set。
const mySet = new Set(['foo', 'bar', 'baz']);
mySet.forEach((value, index, set) => {
console.log(value, index, set);
});此方法将打印 Set 集合中的每个元素。value 参数是 Set 中当前元素的值,index 参数代表元素的索引,set 参数是 Set 集合本身。
Set.prototype.clear() 方法
Set 中的 clear 方法用于删除集合中的所有元素。
const mySet = new Set(['foo', 'bar', 'baz']);
mySet.clear();
console.log(mySet); // 输出 Set(0) {}Set.prototype.values() 方法
在 Set 中,可以使用 Set.prototype.values 方法返回一个该集合中所有值的迭代器。
const mySet = new Set(['foo', 'bar', 'baz']); const values = mySet.values(); console.log(values.next().value); // 输出 foo console.log(values.next().value); // 输出 bar console.log(values.next().value); // 输出 baz
Set.prototype.entries() 方法
此方法返回一个迭代器,其中包含集合中每个元素的一个数组(包括值和键)。
const mySet = new Set(['foo', 'bar', 'baz']); const entries = mySet.entries(); console.log(entries.next().value); // 输出 ["foo", "foo"] console.log(entries.next().value); // 输出 ["bar", "bar"] console.log(entries.next().value); // 输出 ["baz", "baz"]
在此示例中,我们可以看到每个数组都有两个相同的值。这是因为 Set 中的值同时充当键。
Set.prototype.keys() 方法
Set.prototype.keys() 方法返回一个新的迭代器对象,该对象按插入顺序包含Set对象中的所有元素的值的密集数组。
const mySet = new Set(['foo', 'bar', 'baz']); const keys = mySet.keys(); console.log(keys.next().value); // 输出 foo console.log(keys.next().value); // 输出 bar console.log(keys.next().value); // 输出 baz
Set.prototype.add() 方法
在 ES10 中,Set 的添加方法 add 支持链式方法调用。
const mySet = new Set()
.add('foo')
.add('bar')
.add('baz');
console.log(mySet); // 输出 Set(3) {"foo", "bar", "baz"}Set.prototype.delete() 方法
delete() 方法用于从 Set 集合中删除元素。
const mySet = new Set(['foo', 'bar', 'baz']);
mySet.delete('foo');
console.log(mySet); // 输出 Set(2) {"bar", "baz"}Set.prototype.union() 方法
在 ES10 中,Set 声明了一个 union 方法,用于将多个 Set 集合结合起来。
const mySet1 = new Set(['foo', 'bar', 'baz']);
const mySet2 = new Set(['bar', 'baz', 'qux']);
const unionSet = new Set([...mySet1, ...mySet2]);
console.log(unionSet); // 输出 Set(4) {"foo", "bar", "baz", "qux"}Set.prototype.intersection() 方法
与 union() 相反,intersection() 方法用于提取几个 Set 集合的公共部分。
const mySet1 = new Set(['foo', 'bar', 'baz']);
const mySet2 = new Set(['bar', 'baz', 'qux']);
const intersectSet = new Set(
[...mySet1].filter((x) => mySet2.has(x))
);
console.log(intersectSet); // 输出 Set(2) {"bar", "baz"}Set.prototype.difference() 方法
difference() 方法返回两个 Set 集合之间的差异。
const mySet1 = new Set(['foo', 'bar', 'baz']);
const mySet2 = new Set(['bar', 'baz', 'qux']);
const diffSet = new Set(
[...mySet1].filter((x) => !mySet2.has(x))
);
console.log(diffSet); // 输出 Set(1) {"foo"}以上就是 ES10 添加的 Set 新增功能。使用 Set 对象可以大大简化开发人员对数据集合的处理,同时避免了混淆和重复,并可以将数据组织得更加清晰。在使用 Set 过程中,请根据实际情况灵活选择使用上述方法。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/67d5be0aa941bf7134af06e5