## 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
Setzt:
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 & Sets: 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: Kann niemals Duplikate haben, kann jedoch gemischte Datentypen enthalten.
Am häufigsten iterierbar ist Array. Ex. Syntax: 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) {}
Das obige ist der detaillierte Inhalt vonSets. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!