当前位置:网站首页>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))
边栏推荐
- POI及EasyExcel
- MAML原理讲解和代码实现
- MySQL stored procedure study notes (based on 8.0)
- 基于子空间结构保持的迁移学习方法MLSSM
- 格拉姆角场GAF将时序数据转换为图像并应用于故障诊断
- Multi-threaded sequential output
- Memory limit should be smaller than already set memoryswap limit, update the memoryswap at the same
- Microsoft Store 微软应用商店无法连接网络,错误代码:0x80131500
- A semi-supervised Laplace skyhawk optimization depth nuclear extreme learning machine for classification
- 为什么不使用VS管理QT项目
猜你喜欢

数据库:整理四个实用的SQLServer脚本函数

C# 剪裁图片内容区域

ZYNQ之FPGA LED 灯闪烁实验

狗都能看懂的Vision Transformer的讲解和代码实现

Network skills: teach you to install batteries on the router, you can still surf the Internet when the power is cut off!

Faster RCNN原理及复现代码

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

Database document generation tool V1.0

数据库知识:SQLServer创建非sa用户笔记

IoU, GIoU, DIoU and CIoU in target detection
随机推荐
Memory Management
golang rtsp拉流测试
Faster - RCNN principle and repetition code
FCN——语义分割的开山鼻祖(基于tf-Kersa复现代码)
Logical Address & Physical Address
VS 2017编译 QT no such slot || 找不到*** 问题
IP 核之 MMCM/PLL 实验
RHCE之路----全
桌面右键的NVIDIA去除与恢复
Stream API
SENet detailed explanation and Keras reproduction code
Microsoft computer butler 2.0 beta experience
Network skills: teach you to install batteries on the router, you can still surf the Internet when the power is cut off!
基于时序模式注意力机制(TPA)的长短时记忆(LSTM)网络TPA-LSTM的多变量输入风电功率预测
新冠病毒和网络安全的异同及思考
Online public account article content to audio file practical gadget
SENet详解及Keras复现代码
JUC锁框架——初识AQS
让src文件夹能读取xml文件
Time Series Forecasting Based on Reptile Search RSA Optimized LSTM