Learning about new collection types
ES6 has two new collection types, Set
and Map
, which can be useful for certain specific scenarios. In this section, we will learn about these two types and how to write code for them so that we can use them later when we start building our app.
Set
Set
is a collection of unique values or objects. This is a good function to use when you simply want to see whether an item is contained in a large complex list. Let's see an example. Create a new file called set.js
and add the following code:
const userIds = [ 1,2,1,3 ] const uniqueIds = new Set(userIds); console.log(uniqueIds); uniqueIds.add(10); console.log('add 10', uniqueIds); console.log('has', uniqueIds.has(3)); console.log('size', uniqueIds.size); for (let item of uniqueIds) { console.log('iterate', item); }
There are many members for the Set
object, but these are some of its most important...