当前位置:网站首页>关于new Set( )还有哪些是你不知道的
关于new Set( )还有哪些是你不知道的
2022-07-01 18:54:00 【Fx_cap】
1、Set 特点:
Set对象是值的集合,可以按照插入的顺序迭代它的元素- Set 中的元素只会出现一次,即 Set 中的元素是唯一的,所以需要判断两个值是否相等
- Set 中+0和 -0 是不同的值,可以使用 Object.is(+0,-0)进行判断返回false ,+0 严格相等于 -0
- Set 对象允许你存储任何类型的唯一值,无论是原始值或者是对象引用
Set 中NaN和undefined都可以被存储在 Set 中,但是值得注意的是,尽管 NaN !== NaN,但是NaN之间在Set里却被视为相同的值(NaN 被认为是相同的,所以只能存一个)
const set1 = new Set('abc')//{'a','b','c'}
set1.add('abc')//{'a','b','c','abc'}
const set2 = new Set('aaa')//{'a'}
set2.add(undefined)//{'a',undefined}
const set3 = new Set(['abc'])//{'abc'}
set3.add(NaN)//{'abc',NaN}
2、Set 的实例属性:
size: 返回 Set 对象中的值的个数
const set1 = new Set('abc')
console.log(set1.size)// 3
const set2 = new Set('aaa')
console.log(set2.size)// 13、Set 的实例方法:
1)、add( value ): 在Set对象尾部添加一个元素, 返回该 Set 对象
2)、has(value): 判断Set对象中是否有一个元素,返回true/false
3)、clear( ): 移除Set对象内的所有元素
4)、delete( value ): 移除值为 value 的元素,并返回一个布尔值来表示是否移除成功 并且 has(value) 会在此之后返回 false
6)、entries(): 返回一个新的迭代器对象,该对象包含 Set 对象中的按插入顺序排列的所有元素的值的 [value, value] 数组。为了使这个方法和 Map 对象保持相似, 每个值的键和值相等
7)、forEach(( )=>{ }): 按照插入顺序,为 Set 对象中的每一个值调用一次 callBackFn
8)、keys( ):与 values() 方法相同,返回一个新的迭代器对象,该对象包含 Set 对象中的按插入顺序排列的所有元素的值
9)、values( ) : 返回一个新的迭代器对象,该对象包含 Set 对象中的按插入顺序排列的所有元素的值
let set = new Set();
set.add(1); // Set [ 1 ]
set.add(5); // Set [ 1, 5 ]
set.add(5); // Set [ 1, 5 ]//不变,因为已经存在一个值为 5 的成员了
set.add("abc"); // [ 1, 5, "abc" ]
const obj = {a: 1, b: 2};
set.add(obj);//
set.add({a: 1, b: 2}); // obj 指向的是不同的对象,所以可以添加进去
set.has(1); // true
set.has(3); // false
set.has(5); // true
set.has(Math.sqrt(25)); //true Math.sqrt(25) 找25的平方根
set.has("Abc".toLowerCase()); // true 敏感区分大小写
set.has(obj); // true
set.size; // 5
set.delete(5); // true,从 set 中移除 5
set.has(5); // false, 5 已经被移除
set.size; // 4,刚刚移除一个值
for (let item of mySet){
console.log(item) //按顺序输出 : 1, "abc", {"a": 1, "b": 2}, {"a": 1, "b": 2}
}
for (let item of mySet.keys()) {
console.log(item) //按顺序输出 : 1, "abc", {"a": 1, "b": 2}, {"a": 1, "b": 2}
}
for (let item of mySet.values()) {
console.log(item) //按顺序输出 : 1, "abc", {"a": 1, "b": 2}, {"a": 1, "b": 2}
}
//(key value 相等)
for (let [key, value] of mySet.entries()) {
console.log(item) //按顺序输出 : 1, "abc", {"a": 1, "b": 2}, {"a": 1, "b": 2}
}
// 使用 Array.from 转换 Set 为 Array
var arr = Array.from(set); // [1, "abc", {"a": 1, "b": 2}, {"a": 1, "b": 2}]
// Set 和 Array 互换
set2 = new Set([1, 2, 3, 4]);
set2.size;// 4
[...set2]; // [1,2,3,4]
// 用 forEach 迭代
set.forEach(function(value) {
console.log(value);// 依次输出 a,b,c
});4、应用:实现常见的基本集合
const setA = new Set([1, 2, 3, 4]);
const setB = new Set([2, 3]);
const setC = new Set([3, 4, 5, 6]);子集
const isSuperset = (set1, set2) => { for (const item of set2) { if (!set1.has(item)) { return false; } } return true; }; console.log('判断是否是子集', isSuperset(setA, setB));//true并集
const union = (set1, set2) => { const res = new Set(set1); for (const item of set2) { if (!set1.has(item)) { res.add(item); } } return res; }; console.log('返回并集', union(setA, setC));// Set [1, 2, 3, 4, 5, 6]交集
const intersection = (set1, set2) => { const res = new Set(); for (const item of set2) { if (set1.has(item)) { res.add(item); } } return res; }; console.log('返回交集', intersection(setA, setC));// Set [3, 4]不同的成员组成一个新的集合
const symmetricDifference = (set1, set2) => { const res = new Set(set1); for (const item of set2) { if (res.has(item)) { res.delete(item); } else { res.add(item); } } return res; }; console.log( '不同的成员组成一个新的集合', symmetricDifference(setA, setC) );// Set [1, 2, 5, 6]A中剔除B的成员
const difference = (set1, set2) => { const res = new Set(set1); for (const item of set2) { res.delete(item); } return res; }; console.log('A中剔除B的成员', difference(setA, setC));// Set [1, 2 ]
边栏推荐
- 新增订单如何防止重复提交
- qobject_cast用法
- Win11怎么关闭开机自启动软件
- 一文读懂C语言中的结构体
- STC 32位8051单片机开发实例教程 二 I/O工作模式及其配置
- AAAI2020: Real-time Scene Text Detection with Differentiable Binarization
- Mo Tianlun salon | Tsinghua qiaojialin: Apache iotdb, originated from Tsinghua, builds an open source ecological road
- [research materials] iResearch tide Watching: seven major trends in the clothing industry - Download attached
- [research materials] national second-hand housing market monthly report January 2022 - Download attached
- 1592 example 1 King (sgu223 loj10170 luogu1896 increase + / provincial election -) violent thinking pressure DP 01 Backpack
猜你喜欢

Redis installation and startup in Windows environment (background startup)

Mo Tianlun salon | Tsinghua qiaojialin: Apache iotdb, originated from Tsinghua, builds an open source ecological road

Test self-study people must see: how to find test items in software testing?

【多线程】锁策略

漏洞复现-.Net-ueditor上传

HLS4ML进入方法

Related concepts of cookies and sessions

SQL getting started plan-1-select

优质笔记软件综合评测和详细盘点(一) Notion、Obsidian、RemNote、FlowUs

简单但现代的服务器仪表板Dashdot
随机推荐
The large list set is divided into multiple small list sets in equal proportion
Anaconda installs the virtual environment to the specified path
独家消息:阿里云悄然推出RPA云电脑,已与多家RPA厂商开放合作
Win11如何取消任务栏隐藏?Win11取消任务栏隐藏的方法
Arduino Stepper库驱动28BYJ-48步进电机测试程序
有意思了!数据库也搞Serverless!
C # joint Halcon application - Dahua camera acquisition class
渗透工具-TrustedSec 公司的渗透测试框架 (PTF)
Set object value changes null value object
list分割成满足和不满足条件的集合(partitioningBy)
Easycvr accesses the equipment through the national standard gb28181 protocol. What is the reason for the automatic streaming of the equipment?
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
JS的Proxy
Interview question 1
Modsim basic use (Modbus simulator)
CMU AI PhD first year summary
振弦采集模块测量振弦传感器的流程步骤
JVM memory model
February 15, 2022: sweeping robot. There is a floor sweeping robot in the room (represented by a grid). Each grid in the grid has two possibilities: empty and obstacles. The sweeping robot provides fo
1592 example 1 King (sgu223 loj10170 luogu1896 increase + / provincial election -) violent thinking pressure DP 01 Backpack