## 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
套裝:
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 & 套: 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:儘管可以保存混合資料類型,但永遠無法重複。
最常見的可迭代物件是數組。前任。語法:new Set(可迭代)
.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) {}
以上是套的詳細內容。更多資訊請關注PHP中文網其他相關文章!