This is the 25th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
Preface:
Review the previous article on the important data structure of JS JSON understanding/use of processed data to interact, often used in the use of more. In this paper, to record and learn another data structure: Set and WeakSet
In the previous series of articles, we have studied some knowledge points of JavaScript, but also through THE JS implementation of classical algorithms for specific learning, summary more reading -list & classic sorting algorithm -list
JavaScript data structure
ES6 new data structure: Set and WeakSet
Set
The Set | MDN Set object allows you to store any type of unique value, whether the original value or object reference.
A Set is similar to an array, but the values of its members are unique and have no duplicate values. Note that the Set is capitalized to denote a constructor, that is, the Set itself is a constructor, used to generate the Set data structure
let set = new Set(a); [1.3.5.3.7.5.9].forEach(p= > set.add(p));
for (let i of set) {
console.log(i);
}
// 1 3 5 7 9
Copy the code
// When we look at the output here, we forEach each element of the array to the set, but the output set has been de-duplicated, indicating that the set structure does not add duplicate values to the generated data
One caveat: in the Set structure, two Nans are considered to be the same value (whereas NaN! == NaN)
What about null and undefined?
let set = new Set(a); [1.null.5.null.7.undefined.9.undefined.NaN.8.NaN].forEach(p= > set.add(p));
for (let i of set) {
console.log(typeof(i));
}
set
Copy the code
Set constructor
The simple example above simply builds an empty Set of type data using Set()
The Set() function can take an array (or some other data structure with an Iterable interface) as an argument for initialization
let set = new Set([1.3.5.6.6.6.7]);
[...set]
// set: [1, 3, 5, 6, 7]
// Pass an argument to another data structure:
let set = new Set(document.querySelectorAll('div'));
set.size / / 56
Copy the code
Read more:
Classical sorting algorithm:
- 【 jS-sort algorithm -sort()】,
- 【JavaScript- sort algorithm – Hill sort 】
- 【JS- sorting algorithm – merge sort 】,
- 【JavaScript- sorting algorithm – counting sort 】
- 【JS- sort algorithm – bubble sort 】
- JS- Classical sorting algorithm – Selection sort,
- 【JS implementation – classical sorting algorithm – insert sort 】
- JS implementation – classical sorting algorithm -JS implementation of radixSort (radixSort)
- Learn the classic sort algorithm -JS to implement quickSort
- Learn sorting algorithm skills – three – way fast sorting
JavaScript learning and Improvement
- 】 【 Array. The prototype. The map (),
- JS- special symbol – bit operator
- 【ES6 – for/of】,
- JS- Logical operator – short circuit? ,
- 【JS- arrow function 】
- 】 【 JavaScript – forEach (),
- 【JS- Important data type -JSON】
- Js-data structure -JSON- Processing using – Front and back end interactive transfer of data