当前位置:网站首页>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
边栏推荐
- 剑指 Offer II 109. 开密码锁
- 使用Xilinx MIG验证硬件DDR设计
- 2022年中总结
- Application of time series database in bridge monitoring field
- Libgdx learning path 01: libgdx introduction and running tutorial
- The difference between --save Dev and --save in NPM
- R语言与数据分析实战11-数据的删除
- 优麒麟系统安装BeyondComare
- Tikz draw Gantt chart in FJSP -trans necessary
- FTM module of K60: configure motor, encoder and steering gear
猜你喜欢

架构实战营第8模块作业

How many of the top ten test tools in 2022 do you master

FTM module of K60: configure motor, encoder and steering gear

Fantasy 5 (ue5) game engine complete course 2022

Fundamentals of software testing and development | practical development of several tools in testing and development

6-20漏洞利用-proftpd测试
![[image segmentation] vein segmentation based on directional valley detection with matlab code](/img/82/7b7b761c975cd5c2f5b8f3e43592d2.png)
[image segmentation] vein segmentation based on directional valley detection with matlab code

Learn from Li Mu, deep learning - linear regression and basic optimization function

Solve the critical path in FJSP - with Matlab source code

About ASM redundancy
随机推荐
Pytest custom hook function
FTM module of K60: configure motor, encoder and steering gear
Web 3.0 development learning path
ICLR21(classification) - 未来经典“ViT” 《AN IMAGE IS WORTH 16X16 WORDS》(含代码分析)
Application of TSDB in civil aircraft industry
Time waits for no man. The rise of TSDB is at the right time
[machine learning] support vector machine classification
Creating new projects and adding your own programs
Youqilin system installation beyondcomare
2022年最火的十大测试工具,你掌握了几个
6-20漏洞利用-proftpd测试
Application of time series database in bridge monitoring field
Parity rearrangement of Bm14 linked list
sudo rosdep init 出现 ERROR: cannot download default
Pytorch GPU yolov5 reports an error
R语言与数据分析实战11-数据的删除
Method of win7 system anti ARP attack
Self cultivation of Electronic Engineers - when a project is developed
Adobe XD web design tutorial
用LEX(FLEX)生成PL语言的词法分析器