当前位置:网站首页>JS method of changing two-dimensional array to one-dimensional array
JS method of changing two-dimensional array to one-dimensional array
2022-06-30 12:53:00 【Sister Chunfeng】
Method 1 :
utilize es5 Of reduce Realization .
var arr1 = [[0, 1], [2, 3], [4, 5]];
var arr2 = arr1.reduce(function (a, b) {
return a.concat(b)
});
Method 2 :
adopt Array.isArray Determine whether it is a multidimensional array , If it is , Do recursive operations .
var arr1 = [[1,2],3,[[[4], 5]]];
function flatten(arr) {
return [].concat( ...arr.map(x =>
Array.isArray(x) ? flatten(x) : x
))
}
var arr2 = flatten(arr1);
Method 3 :
utilize apply Realization .
var arr1 = [[0, 1], [2, 3], [4, 5]];
var arr2 = [].concat.apply([], arr1); // perhaps Array.prototype.concat.apply([], arr1);
边栏推荐
- 黑马笔记---集合(Collection的常用方法与遍历方式)
- Flink sql控制台,不识别group_concat函数吗?
- Idea 2021.3 golang error: rning: undefined behavior version of delve is too old for go version 1.18
- Open source of xinzhibao applet
- Double dqn notes
- 力扣之螺旋矩阵,一起旋转起来(都能看懂)
- 【C语言深度解剖】float变量在内存中存储原理&&指针变量与“零值”比较
- Unity脚本的基础语法(5)-向量
- FFMpeg AVBufferPool 的理解与掌握
- 【OpenGL】OpenGL Examples
猜你喜欢

Q-learning notes

【C】深入理解指针、回调函数(介绍模拟qsort)

postman 自动生成 curl 代码片段

黑马笔记---集合(Collection的常用方法与遍历方式)

【惊了】迅雷下载速度竟然比不上虚拟机中的下载速度

After the market value evaporated by 65billion yuan, the "mask king" made steady medical treatment and focused on condoms

Efficient elliptic curve point addition and multiplication in scrypt

数据仓库建设之确定主题域

【一天学awk】内置变量的使用

Apple executives openly "open the connection": Samsung copied the iPhone and only added a large screen
随机推荐
Four ways for flinksql to customize udtf
【一天学awk】运算符
项目中遇到一个有趣的事情
JMeter's performance test process and performance test focus
[one day learning awk] Fundamentals
Dataworks synchronizes maxcomputer to sqlserver. Chinese characters become garbled. How can I solve it
Problems and analysis in JMeter performance testing. How many problems have you encountered?
排查问题的方法论(适用于任何多方合作中产生的问题排查)
Package tronapi wave field interface based on thinkphp5 PHP version -- interface document attached -20220627
Motor control Clarke( α/β) Derivation of equal amplitude transformation
市值蒸发650亿后,“口罩大王”稳健医疗,盯上了安全套
Qt中的事件处理
[Select] resource realization information, news, we media, blog applet (can be drained, open traffic master, with PC background management)
7 lightweight and easy-to-use tools to relieve pressure and improve efficiency for developers, and help enterprises' agile cloud launch | wonderful review of techo day
Mysql根据经纬度查询半径多少以内的数据,画个圈圈查数据库
Kubeedge's core philosophy
uniapp支付之APP微信支付unicloud版(附源码)
Matlab tips (22) matrix analysis -- stepwise regression
Open source of xinzhibao applet
The spiral matrix of the force buckle rotates together (you can understand it)