This is the 11th day of my participation in the August More Text Challenge
The Object type in JavaScript is a generic data structure that can be used to map the attribute names of objects to arbitrary values. Objects are actually often used as mappings and collections, but this is limited by the fact that the attributes must be strings, and is complicated by the fact that objects often inherit attributes called “toString”, which are usually not part of a map or collection. So ES6 introduces real Set and Map classes, and today we’ll look at the Set class.
The Set class
A collection is a collection of values, just like an array. However, unlike arrays, sets are not sorted or indexed, and they are not allowed to be repeated: a value is either a member of the set or not a member; It is impossible to ask how many times a value occurs in a collection.
Create a Set object using the Set() constructor:
let s = new Set(a);// a new empty set
let t = new Set([1, s]); // a new set with 2 members
Copy the code
The arguments to the Set() constructor need not be arrays: any iterable (including other sets) is allowed:
let t = new Set(s); // Copy a new set of s elements.
let unique = new Set("Mississippi"); // 4 elements: M, I, s, and p
Copy the code
The size property of a collection is similar to the length property of an array: it tells you how many values the collection contains:
unique.size / / = > 4
Copy the code
The collection does not need to be initialized when it is created. You can add and remove elements at any time using add(), delete(), and clear(). Collections cannot contain duplicates, so adding a value to a collection when it already contains the value has no effect:
let s = new Set(a);/ / empty collection
s.size / / = > 0
s.add(1); // Add a number
s.size / / = > 1; Now the set has one member
s.add(1); // Add the same number again
s.size / / = > 1; The size of the same
s.add(true); // Add another value; Note that mixed types can be added
s.size / / = > 2
s.add([1.2.3]); // Add an array value
s.size / / = > 3; An array has been added, not its elements
s.delete(1) // => true: Element 1 is deleted successfully
s.size // => 2: The size is reduced to 2
s.delete("test") // => false: Test is not a member and fails to be deleted
s.delete(true) // => true: The vm is successfully deleted
s.delete([1.2.3]) // => false: is not the same as the array in the collection
s.size // => 1: There is still an array in the collection
s.clear(); // Clear the collection
s.size / / = > 0
Copy the code
The add() method takes one argument; If you pass an array, it adds the array itself to the collection rather than individual array elements. Add () always returns the collection from which it was called, so if you want to add multiple values to a collection, you can use chained method calls, such as s.addd (‘ a ‘).add(‘ b ‘).add(‘ c ‘); . The delete() method also deletes only one collection element at a time. However, unlike add(), delete() returns a Boolean value. If the specified value is actually a member of the collection, delete() will delete it and return true. Otherwise, it does nothing and returns false.
The most important thing to do with a collection is not to add or remove elements from the collection, but to check whether the specified value is a member of the collection. Use the has() method to do this:
let oneDigitPrimes = new Set([2.3.5.7]);
oneDigitPrimes.has(2) // => true: 2 is a one-digit prime number
oneDigitPrimes.has(3) // => true: 3 is also true
oneDigitPrimes.has(4) // => false: 4 is not prime
oneDigitPrimes.has("5") // => false: "5" is not even a number
Copy the code
The most important thing about collections is that they are optimized for membership testing, and the HAS () method is very fast no matter how many members the collection has. The includes() method of the array also performs membership tests, but the time required is proportional to the size of the array, and using an array as a collection can be much slower than using a real collection object.
The Set class is iterable, which means you can use a for/of loop to enumerate all the elements of the collection:
let sum = 0;
for (let p of oneDigitPrimes) { // Loop through an array of one-digit prime numbers
sum += p; / / sum
}
sum // => 17: 2 + 3 + 5 + 7
Copy the code
Since collections are iterable, you can use the extension operator (…) Convert them to arrays or argument lists:
[...oneDigitPrimes] // => [2,3,5,7]: converts to a collection of arrays
Math.max(... oneDigitPrimes)// => 7: Collection elements are passed as function arguments
Copy the code
Collections are often described as “unordered collections”. However, this is not entirely true for JavaScript collection classes. JavaScript collections have no indexes: you can’t ask for the first or third element of a collection like an array. But the Set class always remembers the order in which elements are inserted, and always uses that order when iterating through the collection: the first element inserted will be the element of the first iteration (assuming you don’t delete it first), and the most recently inserted element will be the element of the last iteration.
In addition to being iterable, the Set class implements a forEach() method similar to the array method of the same name: forEach()
let product = 1;
oneDigitPrimes.forEach(n= > { product *= n; });
product // => 210:2 * 3 * 5 * 7
Copy the code
ForEach () of the array passes the array index as the second argument to the specified function. The collection has no index, so this method of the Set class only passes the element value as both the first and second arguments.