当前位置:网站首页>JS array merging, de duplication, dimensionality reduction (es6: extended operator, set)
JS array merging, de duplication, dimensionality reduction (es6: extended operator, set)
2022-07-28 22:28:00 【A little square】
js Array merge 、 duplicate removal 、 Dimension reduction
1. Merge
1.1 Use concat() Merge arrays
function get_concat(collection_a, collection_b) {
return collection_a.concat(collection_b)
}
1.2 Use …( Extension operator ) Merge arrays
function get_concat(collection_a, collection_b) {
return [...collection_a,...collection_b]
}
2. duplicate removal
2.1 Use for
function get_union(array) {
let temp = []
for (let i = 0; i < array.length; i++) {
if (temp.indexOf(array[i]) == -1) {
temp.push(array[i])
}
}
return temp
}
2.2 Use the extension operator 、Set
function get_union(collection) {
return [...new Set(collection)]
}
The train of thought is array=> Set => array
- array=> Set
new Set(array) - The second step Set => array
[…Set]
ps:ES6 Provides a new data structure Set. It's like an array , But the values of the members are unique , There are no duplicate values .
A little set in Mathematics The opposite sex It means : In a collection , Any two elements are considered different , That is, each element can only appear once .
3. Dimension reduction
drop 2 Dimension group reduced to 1 dimension , Use flat()
function double_to_one(collection) {
return collection.flat();
}
3.1 flat() Method
flat() Method recursively traverses the array at a specified depth , And all the elements and the elements in the traversed sub array are merged into a new array to return .
grammar
var newArray = arr.flat([depth])
Parameters
depth Optional
Specifies the depth of the structure to extract the nested array , The default value is 1.
Return value
A that contains all the elements in the array and subarray New array .
边栏推荐
- Summary of the use of hash table set and map when leetcode brushes questions
- SQL injection less34 (post wide byte injection + Boolean blind injection)
- Ecmasript 5/6 notes
- Learn kotlin - extension function
- Hcip experiment (12)
- [LiteratureReview]Object Detection and Mapping with Bounding Box Constraints
- npm ERR code ETIMEDOUT npm ERR syscall connect npm ERR errno ETIMEDOUT npm ERR network reques...
- 局域网添加DNS服务器进行域名解析
- CDN working principle
- openresty 请求鉴权
猜你喜欢

Aimbetter insight into your database, DPM and APM solutions

LCR测试仪最为主要的功能和用途都是什么

网易云信 2022Q2 产品补给站,快来获取你的产品补给计划吧!
![[Ruiji takeout project] Day5 - Chapter 6 mobile verification code login](/img/53/c578e0d1428ea569fb412a20019924.png)
[Ruiji takeout project] Day5 - Chapter 6 mobile verification code login

Mysql内置函数
![[CVPR 2021] cylinder3d: cylindrical asymmetric 3D convolution network for LIDAR point cloud segmentation](/img/3d/3def78ec88419712e14cf437e21447.png)
[CVPR 2021] cylinder3d: cylindrical asymmetric 3D convolution network for LIDAR point cloud segmentation

Static details of static members

Chapter 7: drawing rotating cubes

HCIP第七次实验

Hcip seventh experiment
随机推荐
HCIP(13)
示波器发展史中的变化
HYDAC overflow valve db08a-01-c-n-500v
Lin Xiaobin, head of Tencent cloud database, borrowed 100 million yuan to speculate in stocks? Insider: the amount is not true
tutorial/detailed_workflow.ipynb 量化金融Qlib库
Sword finger offer II 058. schedule (medium design segment tree treemap ordered set)
HCIP(8)
[Ruiji takeout] day05 package management business development
Ordinary practice of JS DOM programming
ssh 免密码登录
HCIP(10)
Sword finger offer II 052. flatten binary search tree (simple binary search tree DFS)
静态路由和缺省路由实验
网易云信 2022Q2 产品补给站,快来获取你的产品补给计划吧!
Sword finger offer II 057. the difference between the value and the subscript is within the given range (medium array bucket sort sliding window TreeSet)
Sword finger offer II 056. Sum of two nodes in a binary search tree (simple binary search tree DFS hash table double pointer iterator)
Tensorflow serving high performance machine learning model service system
Hcip seventh experiment
成立不到一年!MIT衍生量子计算公司完成900万美元融资
Ultra detailed visual studio 2019 running littlevgl (lvgl) simulator