当前位置:网站首页>【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 ] ]
边栏推荐
- 微信小程序中,从一个页面跳转到另一个页面后,在返回后发现页面同步滚动了
- 天龙八部TLBB系列 - 关于技能冷却和攻击范围数量的问题
- 善用兵者,藏于无形,90 分钟深度讲解最佳推广价值作品
- Swift tableview style (I) system basic
- 《天天数学》连载58:二月二十七日
- RMS to EAP is simply implemented through mqtt
- Unity particle special effects series - the poison spray preform is ready, and the unitypackage package is directly used - on
- QT timer realizes dynamic display of pictures
- Pagoda panel MySQL cannot be started
- QT event filter simple case
猜你喜欢

Roll up, break through 35 year old anxiety, and animate the CPU to record the function call process

isEmpty 和 isBlank 的用法区别

【小技巧】获取matlab中cdfplot函数的x轴,y轴的数值

Fluent generates icon prompt logo widget
![[C language] the use of dynamic memory development](/img/b7/3337bf0df9232d3a44eaeb46b39c63.png)
[C language] the use of dynamic memory development "malloc"

StaticLayout的使用详解

View Slide

Hard core, have you ever seen robots play "escape from the secret room"? (code attached)

QT event filter simple case

A high density 256 channel electrode cap for dry EEG
随机推荐
ArcGIS Pro 创建要素
leetcode:1200. 最小绝对差
Hard core, have you ever seen robots play "escape from the secret room"? (code attached)
《天天数学》连载58:二月二十七日
天龙八部TLBB系列 - 关于技能冷却和攻击范围数量的问题
Swift tableview style (I) system basic
高级 OpenCV:BGR 像素强度图
B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条...
H. 265 introduction to coding principles
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
善用兵者,藏于无形,90 分钟深度讲解最佳推广价值作品
Those who are good at using soldiers, hide in the invisible, and explain the best promotional value works in depth in 90 minutes
Single chip microcomputer principle and Interface Technology (esp8266/esp32) machine human draft
Comparison of batch merge between Oracle and MySQL
驱动制造业产业升级新思路的领域知识网络,什么来头?
天龙八部TLBB系列 - 关于包裹掉落的物品
Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 -下
Design of stepping motor controller based on single chip microcomputer (forward rotation and reverse rotation indicator gear)
【系统设计】指标监控和告警系统
Tianlong Babu TLBB series - questions about skill cooling and the number of attack ranges