当前位置:网站首页>【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 ] ]
边栏推荐
- > Could not create task ‘:app:MyTest. main()‘. > SourceSet with name ‘main‘ not found. Problem repair
- Implementation of smart home project
- 钉钉、企微、飞书学会赚钱了吗?
- How Windows bat script automatically executes sqlcipher command
- The horizontally scrolling recycleview displays five and a half on one screen, lower than the average distribution of five
- TypeError: Cannot read properties of undefined (reading ‘cancelToken‘)
- Apache dolphin scheduler system architecture design
- Detailed explanation of the use of staticlayout
- 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
- 让AI替企业做复杂决策真的靠谱吗?参与直播,斯坦福博士来分享他的选择|量子位·视点...
猜你喜欢
历史上的今天:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生...
RMS TO EAP通过MQTT简单实现
Application of data modeling based on wide table
Getting started with Apache dolphin scheduler (one article is enough)
Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 - 上
MySQL字符类型学习笔记
【小技巧】获取matlab中cdfplot函数的x轴,y轴的数值
. Net delay queue
ConstraintLayout的流式布局Flow
Detailed explanation of the use of staticlayout
随机推荐
Zblogphp breadcrumb navigation code
Apache dolphin scheduler system architecture design
MySQL character type learning notes
Fluent generates icon prompt logo widget
The Alipay in place function can't be found, and the Alipay in place function is offline
How to judge that the thread pool has completed all tasks?
Uncover the practice of Baidu intelligent testing in the field of automatic test execution
Mysql80 service does not start
MySQL字符类型学习笔记
卷起來,突破35歲焦慮,動畫演示CPU記錄函數調用過程
Application of data modeling based on wide table
Apache DolphinScheduler 入门(一篇就够了)
硬核,你见过机器人玩“密室逃脱”吗?(附代码)
Kotlin compose and native nesting
程序员搞开源,读什么书最合适?
(1) Complete the new construction of station in Niagara vykon N4 supervisor 4.8 software
Optimize database queries using the cursor object of SQLite
【OpenCV 例程200篇】219. 添加数字水印(盲水印)
面试:Bitmap像素内存分配在堆内存还是在native中
最全是一次I2C总结