当前位置:网站首页>【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 ] ]
边栏推荐
- Openes version query
- Personal website construction tutorial | local website environment construction | website production tutorial
- Apache dolphin scheduler system architecture design
- 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
- [论文阅读] CKAN: Collaborative Knowledge-aware Atentive Network for Recommender Systems
- Swift set pickerview to white on black background
- Zblogphp breadcrumb navigation code
- 学习笔记5--高精地图解决方案
- 一种用于干式脑电图的高密度256通道电极帽
- Theme. AppCompat. Light. Darkactionbar not found
猜你喜欢
Kotlin Compose 多个条目滚动
Common fault analysis and Countermeasures of using MySQL in go language
Kotlin Compose 与原生 嵌套使用
QT realizes signal transmission and reception between two windows
(1) Complete the new construction of station in Niagara vykon N4 supervisor 4.8 software
Advanced opencv:bgr pixel intensity map
学习笔记5--高精地图解决方案
伪类元素--before和after
A high density 256 channel electrode cap for dry EEG
【小技巧】获取matlab中cdfplot函数的x轴,y轴的数值
随机推荐
Node red series (29): use slider and chart nodes to realize double broken line time series diagram
How to use sqlcipher tool to decrypt encrypted database under Windows system
橫向滾動的RecycleView一屏顯示五個半,低於五個平均分布
Cerebral Cortex:有向脑连接识别帕金森病中广泛存在的功能网络异常
如何判断线程池已经执行完所有任务了?
Swift tableview style (I) system basic
Theme. AppCompat. Light. Darkactionbar not found
How to get the STW (pause) time of GC (garbage collector)?
[论文阅读] CKAN: Collaborative Knowledge-aware Atentive Network for Recommender Systems
学习笔记6--卫星定位技术(上)
Zblogphp breadcrumb navigation code
钉钉、企微、飞书学会赚钱了吗?
cent7安装Oracle数据库报错
mysql80服务不启动
Android SQLite database encryption
MySQL字符类型学习笔记
Getting started with Apache dolphin scheduler (one article is enough)
Personal website construction tutorial | local website environment construction | website production tutorial
美图炒币半年亏了3个亿,华为被曝在俄罗斯扩招,AlphaGo的同类又刷爆一种棋,今日更多大新闻在此...
La vue latérale du cycle affiche cinq demi - écrans en dessous de cinq distributions moyennes