当前位置:网站首页>JS 二维数组变一维数组的方法
JS 二维数组变一维数组的方法
2022-06-30 12:23:00 【春风姐姐】
方法一:
利用 es5 的 reduce 实现。
var arr1 = [[0, 1], [2, 3], [4, 5]];
var arr2 = arr1.reduce(function (a, b) {
return a.concat(b)
});
方法二:
通过 Array.isArray 判断是否为多维数组,如果是,进行递归操作。
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);
方法三:
利用 apply 实现。
var arr1 = [[0, 1], [2, 3], [4, 5]];
var arr2 = [].concat.apply([], arr1); // 或者 Array.prototype.concat.apply([], arr1);
边栏推荐
- Q-learning notes
- [one day learning awk] array usage
- Clipboardjs - development learning summary 1
- 基于ThinkPHP5封装tronapi-波场接口-PHP版本--附接口文档-20220627
- 浅谈 JMeter 运行原理
- Android development interview real question advanced version (with answer analysis)
- Unity脚本的基础语法(3)-访问游戏对象组件
- Scratch drawing square electronic society graphical programming scratch grade examination level 2 true questions and answers analysis June 2022
- Substrate 源码追新导读: Pallet Alliance 并入主线,
- Apple executives openly "open the connection": Samsung copied the iPhone and only added a large screen
猜你喜欢

Visual Studio配置Qt并通过NSIS实现项目打包

Redis-缓存问题

【一天学awk】正则匹配

Vision based robot grasping: from object localization, object pose estimation to parallel gripper grasping estimation

ECDSA signature verification in crypt

Js根据相同值将数组转换为二维数组

rpm2rpm 打包步骤

Basic interview questions for Software Test Engineers (required for fresh students and test dishes) the most basic interview questions

Instructions for legend use in SuperMap iclient3d 11i for cesium 3D scene
![[qnx hypervisor 2.2 user manual]6.2.3 communication between guest and external](/img/ca/9065325ce8882d95fb24c82fb62abc.png)
[qnx hypervisor 2.2 user manual]6.2.3 communication between guest and external
随机推荐
你想要的异常知识点都在这里了
Discussion on JMeter operation principle
Flinksql customizes udatf to implement topn
Visual studio configures QT and implements project packaging through NSIS
MATLAB小技巧(22)矩阵分析--逐步回归
RDS MySQL数据迁移PolarDB MySQL费用可以转过去吗?
如何利用AI技术优化独立站客服系统?听听专家怎么说!
“\“id\“ contains an invalid value“
Substrate 源码追新导读: 修复BEEFY的gossip引擎内存泄漏问题, 智能合约删除队列优化
Dataworks synchronizes maxcomputer to sqlserver. Chinese characters become garbled. How can I solve it
Unity脚本的基础语法(1)-游戏对象的常用操作
JMeter性能测试工作中遇到的问题及剖析,你遇到了几个?
Flink SQL console, group not recognized_ Concat function?
[one day learning awk] Fundamentals
Solve numpy core._ exceptions. Ufunctypeerror: UFUNC 'Add' did not contain a loop with signature matching
[300+ continuous sharing of selected interview questions from large manufacturers] column on interview questions of big data operation and maintenance (II)
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
Wechat launched the picture big bang function; Apple's self-developed 5g chip may have failed; Microsoft solves the bug that causes edge to stop responding | geek headlines
Unity脚本的基础语法(5)-向量
【OpenGL】OpenGL Examples