## 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中文网其他相关文章!