## Usecase: To remove duplicate values of arrays. const order = ['pizza','burger','pasta','noodles','pizza','noodles','burger']; const items = new Set(order); items; // All duplicate values are removed const city = new Set("California").size; city; // 8
Ensembles :
const city = new Set("California"); city; // Set(8) { 'C', 'a', 'l', 'i', 'f', 'o', 'r', 'n' }
## Difference between Set & Array: 1. Although looks similar to array, but it has no key-value pairs. Hence, set[0] is invalid. 2. Only a list of unique values, all duplicate removed. 3. Order of element is irrelevant ## Similarities between Arrays & Ensembles: 1. Set has size property, Array has length propery. 2. Set has 'has' method, Array has includes method. const order = ['pizza','burger','pasta','noodles','pizza','noodles','burger']; const items = new Set(order); items; // Set(4) { 'pizza', 'burger', 'pasta', 'noodles' } //Both array and sets are iterables. Hence easier to convert from sets to array. [...items];
Adv : ne peut jamais avoir de doublon, mais peut contenir des types de données mixtes.
L’itérable le plus courant est Array. Ex. Syntaxe : new Set(iterable)
.size; // returns a numerical value .has('name'); // returns a boolean value .add('name'); // returns the set with added value .delete('name'); // returns a boolean value .clear(); // deletes all elements. returns Set(0) {}
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!