ES6: Set
Understand Set
ES6 provides the data structure Set. Similar to an array, but without duplicate values.
Set itself is a constructor used to generate the Set data structure
const s = new Set(); [2, 3, 5, 4, 5, 2, 2].forEach(x => s.add(x));for(let i of s ) { console.log(i); //2 3 4 5 }
Set can accept an array ( Or array-like object) as a parameter, used to initialize
var set = new Set([1, 2, 3, 4, 4]); [...set]; // [1, 2, 3, 4]
can be used for array deduplication
[...new Set(array)]
Array.from()
The method can convert the Set structure into an arrayArray.from(new Set(array))
When adding a value to a Set, no type conversion will occur (similar to exact equality ===), but please note that in the Set NaN is equal to itself. The other two objects are always not equal.
let set = new Set();let a = NaN;let b = NaN; set.add(a); set.add(b); set; //{NaN} 只能加入一个,说明Set内部两个NaN是相等的
Set instance properties and methods
Properties:
Set.prototype.constructor
: Constructor, the default is Set functionSet.prototype.size
: Returns the total number of members of the instanceOperation method (for the specific implementation of the method, see: My simple study of JS collections):
add(value )
: Add a value and return the Set structure itselfdelete(value)
: Delete a value and return a Boolean valuehas(value)
: Returns a Boolean value indicating whether it is a memberclear()
: Clear all members , no return value
s.add(1).add(2).add(2); //链式写法s.size(); //2s.has(3); //falses.delete(2); s.has(2); //false
遍历方法
keys()
:返回键名的遍历器(什么是遍历器?Iterator)values()
:返回键值的遍历器entries()
:返回键值对的遍历器forEach()
:使用回调函数遍历每个成员
这里要注意Set的键名和键值是同一个值,所以key()和values()行为是一致的。
let set = new Set(['red', 'green', 'no']);for(let item of set.keys()) { console.log(item); //red green no}for(let item of set.values()) { console.log(item); //red green no}for(let item of set.entries()) { console.log(item); //['red': 'red'] ['green': 'green'] ['no': 'no']}//对每个成员执行某种操作,参数依次为键值、键名、集合本身new Set([1, 2, 3]).forEach((value, key) => console.log(value * 2)); //2 4 6
操作集合
let a = new Set([1, 2, 3]);let b = new Set([4, 3, 2]);//并集let union = new Set([...a, ...b]); //{1, 2, 3, 4}//交集let intersect = new Set([...a].filter(x => b.has(x))); //{2, 3}//差集let difference = new Set([...a].filter(x => !b.has(x))); //{1}
号外:扩展运算符(...)内部使用for...of循环,所以应该知道for of是干嘛的吧
数组的map()
和filter()
可用于Set
let set = new Set([1, 2, 3]);set = new Set([...set].map(x => x * 2)); //set: {2, 4, 6}let set = new Set([1, 2, 3, 4, 5]);set = new Set([...set].filter(x => (x % 2) == 0)); //set {2, 4}
The above is the detailed content of What is the Set data structure of ES6. For more information, please follow other related articles on the PHP Chinese website!

二叉树是计算机科学中常见的数据结构,也是Java编程中常用的一种数据结构。本文将详细介绍Java中的二叉树结构。一、什么是二叉树?在计算机科学中,二叉树是一种树形结构,每个节点最多有两个子节点。其中,左侧子节点比父节点小,右侧子节点则比父节点大。在Java编程中,常用二叉树表示排序,搜索以及提高对数据的查询效率。二、Java中的二叉树实现在Java中,二叉树

想了解更多关于开源的内容,请访问:51CTO开源基础软件社区https://ost.51cto.com一、栈的概念栈由一系列对象对象组织的一个集合,这些对象的增加和删除操作都遵循一个“后进先出”(LastInFirstOut,LIFO)的原则。在任何时刻只能向栈中插入一个对象,但只能取得或者删除只能在栈顶进行。比如由书构成的栈,唯一露出封面的书就是顶部的那本,为了拿到其他的书,只能移除压在上面的书,如图:栈的实际应用实际上很多应用程序都会用到栈,比如:网络浏览器将最近浏览

PHP是一种广泛使用的脚本语言,被广泛用于Web开发,服务器端编程以及命令行编程等。随着PHP不断更新和发展,它也日益成为一个更强大的编程工具,为用户提供了更多的功能和更多的工具来开发高质量的应用程序。其中,数据结构是一个非常重要的领域,一种有效的数据结构可以大大提高程序的性能和可读性。在这篇文章中,我们将讨论PHP8中支持的新数据结构,这些新的数据结构将让

如何解决Java中遇到的代码性能优化问题随着现代软件应用的复杂性和数据量的增加,对于代码性能的需求也变得越来越高。在Java开发中,我们经常会遇到一些性能瓶颈,如何解决这些问题成为了开发者们关注的焦点。本文将介绍一些常见的Java代码性能优化问题,并提供一些解决方案。一、避免过多的对象创建和销毁在Java中,对象的创建和销毁是需要耗费资源的。因此,当一个方法

随着计算机科学的不断发展,数据结构与算法成为了计算机科学领域中最为基础、重要的模块。数据结构是一种组织和存储数据的方式,它是解决问题的基础。算法则是计算机科学的核心,它是指在计算机程序中解决问题的方法和技术。Java作为一种广泛应用的编程语言,其自带的数据结构和算法库是非常强大的,赋予了开发人员更多的力量。一、数据结构Java中提供了多种数据结构,包括数组

go语言数据结构有四大类:1、基础类型,包括整型(有符号和无符号整数)、浮点数、复数、字符串(由不可变的字节序列构成)、布尔值(只有true和false两个值);2、聚合类型,包括数组、结构体(是由任意个任意类型的变量组合在一起的数据类型);3、引用类型,包括指针、slice(是一个拥有相同元素的可变长度序列)、map、function、channel;4、接口类型。

c语言中,数据结构是指相互之间存在一种或多种特定关系的数据元素的集合,它是计算机存储、组织数据的方式;常见数据结构有:线性数据结构(数组、链表、栈、队列和线性表)、树形结构(二叉树、完全二叉树、二叉查找树、堆)、图形结构(有向图和无向图)。

Python正则表达式是一种基于模式匹配的字符串处理工具,它可以帮助我们快速有效地从文本中提取所需信息。在数据结构和算法中,正则表达式可以用来实现文本匹配、替换、分割等功能,为我们的编程提供更加强大的支持。本文将介绍如何使用Python正则表达式进行数据结构和算法。一、正则表达式的基础知识在开始之前,先了解一下正则表达式的一些基础知识:字符集:用方括号表示,


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools
