当前位置:网站首页>【JS】数组降维
【JS】数组降维
2022-07-05 09:47:00 【一颗不甘坠落的流星】
文章目录
在项目中,我们常常要处理比较复制的数据结构,而又找不到现成的方法,这时候就需要我们手动用 js 原生去封装一个了,这里介绍的是将多维数组降一层结构的方法,比如三维数组转二维数组、二维数组转一维数组。
- 数据结构:
// 三维数组
[
[
[
8,
680
]
],
[
[
6,
681
]
]
]
// 二维数组
[
[
8,
680
],
[
6,
681
]
]
- 方法1:
reduce
+concat
const arr3 = [ [ [ 8, 680 ] ], [ [ 6, 681 ] ] ];
const arr2 = [ [ 8, 680 ], [ 6, 681 ] ];
function arr3ToArr2(arr){
return arr.reduce((acc, val) => acc.concat(val), []);
}
console.log(arr3ToArr2(arr3)) // [ [ 8, 680 ], [ 6, 681 ] ]
- 方法2:
...
+concat
const arr3 = [ [ [ 8, 680 ] ], [ [ 6, 681 ] ] ];
const arr2 = [ [ 8, 680 ], [ 6, 681 ] ];
function arr3ToArr2(arr){
return [].concat(...arr)
}
console.log(arr3ToArr2(arr3)) // [ [ 8, 680 ], [ 6, 681 ] ]
边栏推荐
- MySQL字符类型学习笔记
- . Net delay queue
- QT timer realizes dynamic display of pictures
- Cut off 20% of Imagenet data volume, and the performance of the model will not decline! Meta Stanford et al. Proposed a new method, using knowledge distillation to slim down the data set
- Constrained layout flow
- Livedata interview question bank and answers -- 7 consecutive questions in livedata interview~
- 天龙八部TLBB系列 - 关于包裹掉落的物品
- Hard core, have you ever seen robots play "escape from the secret room"? (code attached)
- The horizontally scrolling recycleview displays five and a half on one screen, lower than the average distribution of five
- MySQL character type learning notes
猜你喜欢
驱动制造业产业升级新思路的领域知识网络,什么来头?
RMS to EAP is simply implemented through mqtt
ArcGIS Pro 创建要素
To bring Euler's innovation to the world, SUSE should be the guide
【系统设计】指标监控和告警系统
mongoDB副本集
自动化规范检查软件如何发展而来?
高级 OpenCV:BGR 像素强度图
Design and Simulation of fuzzy PID control system for liquid level of double tank (matlab/simulink)
Meitu lost 300 million yuan in currency speculation for half a year. Huawei was exposed to expand its enrollment in Russia. Alphago's peers have made another breakthrough in chess. Today, more big new
随机推荐
【小技巧】获取matlab中cdfplot函数的x轴,y轴的数值
[200 opencv routines] 219 Add digital watermark (blind watermark)
Z-blog template installation and use tutorial
横向滚动的RecycleView一屏显示五个半,低于五个平均分布
Implementation of smart home project
Swift uses userdefaults and codable to save an array of class objects or structure instances
卷起來,突破35歲焦慮,動畫演示CPU記錄函數調用過程
宝塔面板MySQL无法启动
(1) Complete the new construction of station in Niagara vykon N4 supervisor 4.8 software
ArcGIS Pro 创建要素
Mysql80 service does not start
Usage differences between isempty and isblank
Hard core, have you ever seen robots play "escape from the secret room"? (code attached)
MySQL数字类型学习笔记
Kotlin Compose 多个条目滚动
leetcode:1200. 最小绝对差
学习笔记5--高精地图解决方案
Design and exploration of Baidu comment Center
Comparison of batch merge between Oracle and MySQL
@JsonAdapter注解使用