The JavaScript Set Object

栏目: IT技术 · 发布时间: 5年前

内容简介:TheTo use set, you create a new instance of it.

Set is one of my favorite built-in object types in JavaScript. Today I’ll introduce the Set object and discuss some of its use cases.

The Set Object

The Set object is a collection of values in which you can store unique primitive values or object references. Uniqueness is key here—no primitive value or object reference will can added multiple times.

How to Use Set

To use set, you create a new instance of it.

const mySet = new Set();

We now have an empty set. We can add the number 1 to this set by using the add method.

mySet.add(1);

How do we know that we’ve added 1 ? We can use the has method to check.

console.log(mySet.has(1));
// true

Let’s add an object reference now and then check that we have that object in our Set .

const obj = { name: 'Daffodil' };
mySet.add(obj);
console.log(mySet.has(obj));
// true

Remember that object references are compared, not the object keys themselves. In other words:

console.log(mySet.has({ name: 'Daffodil' }));
// false

We can see how many elements are in the Set by using the size property.

console.log(mySet.size);
// 2

Next up, let’s remove a value using the delete method.

mySet.delete(1);
console.log(mySet.has(1));
// false

Finally, we’ll clear out the Set using the clear method.

mySet.clear();
console.log(mySet.size);
// 0

Iterating Over a Set

The easiest way to iterate over a Set is to use the forEach method.

new Set([1, 2, 3]).forEach(el => {
  console.log(el * 2);
});
// 2 4 6

Set objects also have entries , keys , and values methods, which each returns Iterators . Those are a bit outside the scope of this tutorial!

Using Sets in the Wild

I find the Set object to be really great for keeping track of a binary state associated with an object. A great example is an accordion menu: each item in the menu will either be open or closed. We can create a Set called isOpen that tracks the open status of an accordion item and a toggle function that toggles the open status:

const isOpen = new Set();

function toggle(menuItem) {
  if (isOpen.has(menuItem)) {
    isOpen.delete(menuItem);
  } else {
    isOpen.add(menuItem);
  }
}

A Note of Efficiency

You might be thinking that the Set object seems awfully similar to arrays. There is, however, a big difference that may have performance ramifications in your application. The Set object is required to be implemented using hash tables (or methods with hash table-like efficiency) [1].

When you store something in an array, you might have to traverse the entire array to find the item. With a Set , however, the lookup is instantaneous. Practically speaking, the performance will be negligable for most cases, but good to remember if you find yourself having to track large numbers of items!

Conclusion

I hope this helped you understand the Set object and you now have a new tool in your JavaScript toolbelt!

References

  1. Set Object Specification

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

免费

免费

[美] 克里斯·安德森 / 蒋旭峰、冯斌、璩静 / 中信出版社 / 2009-9 / 39.00

在《免费:商业的未来 》这本书,克里斯·安德森认为,新型的“免费”并不是一种左口袋出、右口袋进的营销策略,而是一种把货物和服务的成本压低到零的新型卓越能力。在上世纪“免费”是一种强有力的推销手段,而在21世纪它已经成为一种全新的经济模式。 究竟什么是免费商业模式?根据克里斯·安德森的说法,这种新型的“免费”商业模式是一种建立在以电脑字节为基础上的经济学,而非过去建立在物理原子基础上的经济学。......一起来看看 《免费》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码