当前位置:网站首页>ES6 conversion of new data type set and arr set map
ES6 conversion of new data type set and arr set map
2022-07-28 19:23:00 【ca11meback】
var arr=[11,22,33,11]
var set1=new Set(arr)// Traversal array Then call the elements of the array s1.add Add to the collection
var arr2=Array.from(set1)// Take out all the data in the data container Then save the return value of the new array
console.log(arr2)use es A new method is introduced to de duplicate the data
And you can see that set either Grammar sugar only uses standard construction grammar , And set It will automatically go heavy

set1.add(100)
set1.add(100)
set1.add(200)
set1.add("hello")
set1.add([100])
set1.add([100])
console.log(set1)
set1.forEach(function(el){
console.log(el)
}) 
It is not difficult to see that we use add Function to add elements , Although the two arrays are similar, they are not the same reference, so there is no de duplication
// Combine
var arr = [10, 20, 30, 10,{}]
var arr2 = [100, 200,30,10]
var set1 = new Set([...arr,...arr2])
console.log(set1)Union
var arr = [10, 20, 30, 10,{}]
var arr2 = [100, 200,30,10]
var set2 = new Set(arr.filter(function(el) {
if (arr2.includes(el)) {
return el
}
}))
console.log(set2)Find the intersection
It is more difficult to find the intersection Need to use filter Filter and use us includes To check whether it is in the second array
arr-map
The premise is that it must be a two-dimensional array because map The creation parameter of is an array
var arr=[[1,"hello"],[2,"h5"],[3,{name:"karen"}]]
var m1=new Map(arr)map-arr
var arr2=Array.from(m1)
console.log(arr,m1,arr2,arr==arr2,arr[2][1]==arr2[2][1])arr Array built-in function conversion Although the content is the same when converting But it's still the previous problem Are two arrays Different references But the reference data stored inside is the same reference
arr-set
var arr=[100,200,100,{age:20},{age:20}]
var s1=new Set(arr)
console.log(s1,s1.size)There are two objects with the same content but different references, so they won't be duplicated , But the basic data type is de duplicated
set-arr
var arr2=Array.from(s1)
console.log(arr,s1,arr2,arr[4]==arr2[3])The references of the elements inside remain unchanged
边栏推荐
- JDBC simple encapsulation
- BLDC 6步换相 simulink
- Minio distributed file system learning notes
- 3、 Uni app fixed or direct to a certain page
- [filter tracking] target tracking based on EKF, TDOA and frequency difference positioning with matlab code
- Srs4.0 installation steps
- Bm11 list addition (II)
- 2、 Uni app login function page Jump
- 当CNN遇见Transformer《CMT:Convolutional Neural Networks Meet Vision Transformers》
- Libgdx learning path 01: libgdx introduction and running tutorial
猜你喜欢

【滤波跟踪】基于EKF、时差和频差定位实现目标跟踪附matlab代码

It is the best tool to evaluate six kinds of map visualization software in three categories

关于白盒测试,这些技巧你得游刃有余~

CVPR21-无监督异常检测《CutPaste:Self-Supervised Learning for Anomaly Detection and Localization》

How does the mqtt server built with emqx forward data and save it to the cloud database?

Pytorch:快速求得NxN矩阵的主对角线(diagonal)元素与非对角线元素

Application of time series database in museum environment detection

PyG搭建异质图注意力网络HAN实现DBLP节点预测

4、 Interface requests data to update input information interactively

Getting started with QT & OpenGL
随机推荐
[radar] radar signal online sorting based on kernel clustering with matlab code
Web 3.0 development learning path
unity CS1513
BM16 删除有序链表中重复的元素-II
JDBC simple encapsulation
Structure and working principle of thyristor
FTM module of K60: configure motor, encoder and steering gear
Minio distributed file system learning notes
Bm11 list addition (II)
2、 Uni app login function page Jump
Is it useful to learn software testing?
Pytorch:交叉熵损失(CrossEntropyLoss)以及标签平滑(LabelSmoothing)的实现
C language (high-level) character function and string function + Exercise
Overview and working principle of single chip microcomputer crystal oscillator
Kotlin Android development novice tutorial
Server body 21: pre compilation processing by different compilers (a brief introduction to MSVC and GCC)
架构实战营第8模块作业
【图像分割】基于方向谷形检测实现静脉纹路分割附MATLAB代码
Using CPLEX to solve small-scale energy-efficient FJSP
6-20 vulnerability exploitation proftpd test