当前位置:网站首页>set集合
set集合
2022-08-04 05:34:00 【初夏半微凉】
set集合
set和map一样没有语法糖,只有标准写法
var s1 = new Set();
// 存数据
s1.add(100);
s1.add(100);
// 长的一样也不是一个数组,因为开辟了两个内存空间
s1.add([100]);
s1.add([100]);
s1.add(200);
console.log(s1);
// 遍历s1并打印
s1.forEach(function(el){
console.log(el);
})
var arr = [11,22,33,11];
// 遍历数组 然后把数组的元素调用s1.add添加进集合中
var s1 = new Set(arr);
// 取出了数据容器中的所有数据,然后存入新数组返回值
var srr2 = Array.from(s1);
var arr = [10,20,30,10,{
}];
// 把arr的元素全部取出,然后存入新数组arr2中
var arr2 = [...arr];
// 第二个相等,只是保存的对象,不是创建了对象 所以是一样的
console.log(arr2,arr==arr2,arr[4]==arr2[4]) //false true
//并集,两个合并在一起
var arr = [10, 20, 30, 10,{
}]
var arr2 = [100, 200,30,10]
var s1 = new Set([...arr,...arr2])
console.log(s1)
//交集,相同的部分
var arr = [10, 20, 30, 10,{
}]
var arr2 = [100, 200,30,10]
// 使用filter过滤器,来过滤掉不要的数据
var s2 = new Set(arr.filter(function(el) {
if (arr2.includes(el)) {
return el
}
}))
console.log(s2)
数组转map
数组转map:必须是二维数组
map转数组: Array.from()静态方法
var arr=[[1,"hello"],[2,"h5"],[3,{
name:"karen"}]]
var m1=new Map(arr)
var arr2=Array.from(m1)
console.log(arr,m1,arr2,arr==arr2,arr[2][1]==arr2[2][1])
var arr=[100,200,100,{
age:20},{
age:20}]
//数组转集合
var s1=new Set(arr)
console.log(s1,s1.size)
//集合转数组
var arr2=Array.from(s1)
console.log(arr,s1,arr2,arr[4]==arr2[3])
//多个数组转集合和map: ...
var arr1=[10,20]
var arr2=[100,20]
var arr3=[200,{
name:"karen"}]
var s1=new Set([...arr1,...arr2,arr3])
var m1=new Map([arr1,arr2,arr3])
var m2=new Map(m1)
console.log(m1,m2,m1==m2,m1.get(200)==m2.get(200))
边栏推荐
- Database document generation tool V1.0
- 20170729
- Database Skills: Organize SQL Server's Very Practical Scripts
- MySQL stored procedure study notes (based on 8.0)
- Online public account article content to audio file practical gadget
- MAML原理讲解和代码实现
- 读取JDBC配置文件
- Memory Management
- Vmmem process (WSL2) consumes huge amount of memory
- 对渗透测试工程师来说,学历重要嘛?
猜你喜欢

Database document generation tool V1.0

硬件知识:RTMP和RTSP传统流媒体协议介绍

EfficientNet解读:神经网络的复合缩放方法(基于tf-Kersa复现代码)

Pfsense漏洞复现(CVE-2021-41282)

JUC锁框架——基于AQS的实现,从ReentrantLock认识独占和共享

数据库文档生成工具V1.0

为什么不使用VS管理QT项目

Gramm Angle field GAF time-series data into the image and applied to the fault diagnosis

C# 剪裁图片内容区域

杰哥带大家做一次meterpreter内网渗透模拟
随机推荐
自适应迁移学习核极限学习机用于预测
Vmmem process (WSL2) consumes huge amount of memory
注册表设置默认浏览器 win7,winserver 2008,winserver 2012
电脑知识:台式电脑应该选择品牌和组装,值得收藏
更改软件的默认安装位置
Gramm Angle field GAF time-series data into the image and applied to the fault diagnosis
基于Event Stream操作JSON
【音视频开发系列】fdk_aac 之 PCM 转 AAC
golang chan
Memory limit should be smaller than already set memoryswap limit, update the memoryswap at the same
DenseNet详解及Keras复现代码
Microsoft Store 微软应用商店无法连接网络,错误代码:0x80131500
Uos统信系统 DISK(RAID+LVM)
LeetCode刷题
有且仅有的三种处理JSON的方法
无一技之长学什么可以做到月入上万?
Database knowledge: SQLServer creates non-sa user notes
RHCE之路----全
Based on the EEMD + + MLR GRU helped time series prediction
selenium webdriver 防爬问题 C#