当前位置:网站首页>【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 ] ]
边栏推荐
- mongoDB副本集
- Personal website construction tutorial | local website environment construction | website production tutorial
- Comment obtenir le temps STW du GC (collecteur d'ordures)?
- MySQL character type learning notes
- Tianlong Babu TLBB series - about items dropped from packages
- 天龙八部TLBB系列 - 关于包裹掉落的物品
- Mysql80 service does not start
- 一种用于干式脑电图的高密度256通道电极帽
- Kotlin compose and native nesting
- Design and Simulation of fuzzy PID control system for liquid level of double tank (matlab/simulink)
猜你喜欢
学习笔记6--卫星定位技术(上)
Kotlin compose and native nesting
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
Pagoda panel MySQL cannot be started
学习笔记5--高精地图解决方案
Advanced opencv:bgr pixel intensity map
Generics, generic defects and application scenarios that 90% of people don't understand
Swift tableview style (I) system basic
(1) Complete the new construction of station in Niagara vykon N4 supervisor 4.8 software
ArcGIS Pro creating features
随机推荐
Those who are good at using soldiers, hide in the invisible, and explain the best promotional value works in depth in 90 minutes
Swift uses userdefaults and codable to save an array of class objects or structure instances
RMS TO EAP通过MQTT简单实现
钉钉、企微、飞书学会赚钱了吗?
【 conseils 】 obtenir les valeurs des axes X et y de la fonction cdfplot dans MATLAB
天龙八部TLBB系列 - 单体技能群伤
The Alipay in place function can't be found, and the Alipay in place function is offline
Wechat applet - simple diet recommendation (2)
Windows uses commands to run kotlin
【C语言】动态内存开辟的使用『malloc』
Is it really reliable for AI to make complex decisions for enterprises? Participate in the live broadcast, Dr. Stanford to share his choice | qubit · viewpoint
Implementation of smart home project
把欧拉的创新带向世界 SUSE 要做那个引路人
La vue latérale du cycle affiche cinq demi - écrans en dessous de cinq distributions moyennes
一个程序员的职业生涯到底该怎么规划?
面试:Bitmap像素内存分配在堆内存还是在native中
橫向滾動的RecycleView一屏顯示五個半,低於五個平均分布
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
Constraintlayout officially provides rounded imagefilterview
Fluent generates icon prompt logo widget