当前位置:网站首页>多维度数组拉平到一维
多维度数组拉平到一维
2022-08-08 04:25:00 【zjLOVEcyj】
多维度数组拉平到一维
[1, [2,3], [4,[5, [6]]]]
[ 1, 2, 3, 4, 5, 6 ]
- 利用reduce和递归实现
let arr = [1, [2,3], [4,[5, [6]]]]
function flatten (arr) {
return arr.reduce((pre, cur) => pre.concat(Array.isArray(cur) ? flatten(cur) : cur), [])
}
flatten(arr)
- js原生方法
arr.flat(Infinity)
第二个参数是多维数组的维度,可以设置为Infinity,可以对无限多维度数组拉平, 设置为2时,代表将arr拉平2层
譬如arr = [ 1, [ 2, 3 ], [4, [ 5, [ 6 ] ] ] ]
那么arr.flat(2) 就是[ 1, 2, 3, 4, 5, [ 6 ] ]
边栏推荐
- Building a High-Performance Platform on AWS Using Presto and Alluxio to Support Real-Time Gaming Services
- Risk control strategy must be learned | This method of mining rules with decision trees
- C# "Original CSharp" 4th Chapter It is common for people to change in time, but it is difficult to know the succession of humanities
- New User Plane Design and Key Technologies in the 6G Era
- leetcode 112.路经总和 递归
- leetcode 70.爬楼梯 动态规划
- MySQL - Indexes and Transactions
- [Code Analysis] Graph small sample anomaly detection method: GDN: Few-shot Network Anomaly Detection via Cross-network Meta-learning
- vulnhub-DC-3靶机渗透记录
- 牛客多校第6场赛后学习 B(两种做法)G(两种做法)M(两种写法)J
猜你喜欢
随机推荐
The use of mmedicting get_flops. Py
Qt 日志模块的个性化使用
The fledgling Xiao Li's 115th blog project notes on the creation of the domestic GD32F103RCT6 basic project
egg-Alibaba Cloud SMS Configuration
Open3D 基于颜色的ICP配准
类似Bugfree的9大在线缺陷管理软件
The live broadcast of agricultural products continues to heat up, Economic Daily: Don’t forget quality when rushing
07查询表达式 及 page分页、order 排序《ThinkPHP6 入门到电商实战》
剑指Offer 18.删除链表的节点
ToDesk企业版上新 | 十大新功能,让企业远控更安全、更便捷、更流畅
Basic introduction to NLP
10款自媒体人必备的免费工具,快速高效运营
开发如何尽可能的避免BUG
Machine Learning Notes: Learning Rate Warmup
C# "Original CSharp" 4th Chapter It is common for people to change in time, but it is difficult to know the succession of humanities
leetcode: 455. 分发饼干
C语言 扫雷
面向6G的通信感知一体化架构与关键技术
拒绝“内卷”跃迁软件测试最大门槛,我是如何从月薪8K到15K的?
L3-007 ladder map (test point 2 is stuck, you can see it)









