当前位置:网站首页>【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 ] ]
边栏推荐
- Wechat applet - simple diet recommendation (3)
- ThreadLocal source code learning
- Cerebral Cortex:有向脑连接识别帕金森病中广泛存在的功能网络异常
- How to judge that the thread pool has completed all tasks?
- [NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution
- Flutter development: a way to solve the problem of blank space on the top of listview
- 苹果 5G 芯片研发失败?想要摆脱高通为时过早
- Design and exploration of Baidu comment Center
- Coffeescript Chinese character to pinyin code
- Wechat applet - simple diet recommendation (2)
猜你喜欢

Unity particle special effects series - the poison spray preform is ready, and the unitypackage package can be used directly - next

《天天数学》连载58:二月二十七日

学习笔记6--卫星定位技术(上)

Those who are good at using soldiers, hide in the invisible, and explain the best promotional value works in depth in 90 minutes

@SerializedName注解使用

RMS TO EAP通过MQTT简单实现

ArcGIS Pro creating features

Comment obtenir le temps STW du GC (collecteur d'ordures)?

Getting started with Apache dolphin scheduler (one article is enough)

Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 -下
随机推荐
NCP1342芯片替代料PN8213 65W氮化镓充电器方案
Usage differences between isempty and isblank
天龙八部TLBB系列 - 关于包裹掉落的物品
Tianlong Babu TLBB series - questions about skill cooling and the number of attack ranges
MySQL character type learning notes
isEmpty 和 isBlank 的用法区别
钉钉、企微、飞书学会赚钱了吗?
如何写出高质量的代码?
Fluent generates icon prompt logo widget
La vue latérale du cycle affiche cinq demi - écrans en dessous de cinq distributions moyennes
Swift set pickerview to white on black background
MySQL digital type learning notes
天龙八部TLBB系列 - 单体技能群伤
[论文阅读] CKAN: Collaborative Knowledge-aware Atentive Network for Recommender Systems
基于单片机步进电机控制器设计(正转反转指示灯挡位)
Comparison of batch merge between Oracle and MySQL
让AI替企业做复杂决策真的靠谱吗?参与直播,斯坦福博士来分享他的选择|量子位·视点...
Swift tableview style (I) system basic
QT timer realizes dynamic display of pictures
如何判断线程池已经执行完所有任务了?