当前位置:网站首页>[JS] array dimensionality reduction
[JS] array dimensionality reduction
2022-07-05 10:19:00 【A meteor unwilling to fall】
List of articles
In the project , We often have to deal with data structures that are more replicated , And there is no ready-made method , At this time, we need to use it manually js Native to encapsulate one , Here is the method of reducing multidimensional array to one level structure , For example, three-dimensional array to two-dimensional array 、 Two dimensional array to one dimensional array .
- data structure :
// Three dimensional array
[
[
[
8,
680
]
],
[
[
6,
681
]
]
]
// Two dimensional array
[
[
8,
680
],
[
6,
681
]
]
- Method 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 ] ]
- Method 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 ] ]
边栏推荐
- vscode的快捷键
- Energy momentum: how to achieve carbon neutralization in the power industry?
- To bring Euler's innovation to the world, SUSE should be the guide
- 请问postgresql cdc 怎么设置单独的增量模式呀,debezium.snapshot.mo
- Workmanager Learning one
- How does redis implement multiple zones?
- StaticLayout的使用详解
- 《微信小程序-基础篇》小程序中的事件与冒泡
- DDOS攻击原理,被ddos攻击的现象
- @Serializedname annotation use
猜你喜欢
> Could not create task ‘:app:MyTest. main()‘. > SourceSet with name ‘main‘ not found. Problem repair
Unity particle special effects series - the poison spray preform is ready, and the unitypackage package can be used directly - next
Energy momentum: how to achieve carbon neutralization in the power industry?
把欧拉的创新带向世界 SUSE 要做那个引路人
Mysql80 service does not start
B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条...
如何判断线程池已经执行完所有任务了?
【黑马早报】罗永浩回应调侃东方甄选;董卿丈夫密春雷被执行超7亿;吉利正式收购魅族;华为发布问界M7;豆瓣为周杰伦专辑提前开分道歉...
How to plan the career of a programmer?
一个程序员的职业生涯到底该怎么规划?
随机推荐
Today in history: the first e-book came out; The inventor of magnetic stripe card was born; The pioneer of handheld computer was born
Design of stepping motor controller based on single chip microcomputer (forward rotation and reverse rotation indicator gear)
【JS】数组降维
ConstraintLayout的流式布局Flow
面试:List 如何根据对象的属性去重?
Mysql80 service does not start
Matrix processing practice
Pagoda panel MySQL cannot be started
How did automated specification inspection software develop?
To bring Euler's innovation to the world, SUSE should be the guide
《微信小程序-基础篇》小程序中的事件与冒泡
《剑来》语句摘录(七)
How can PostgreSQL CDC set a separate incremental mode, debezium snapshot. mo
Glide Mastery
La vue latérale du cycle affiche cinq demi - écrans en dessous de cinq distributions moyennes
@Jsonadapter annotation usage
@Serializedname annotation use
pytorch输出tensor张量时有省略号的解决方案(将tensor完整输出)
Singleton mode encapsulates activity management class
Events and bubbles in the applet of "wechat applet - Basics"