当前位置:网站首页>Flatten multidimensional array to one dimension
Flatten multidimensional array to one dimension
2022-08-08 04:29:00 【zjLOVEcyj】
Multidimensional array flattened to 1D
[1, [2,3], [4,[5, [6]]]]
[ 1, 2, 3, 4, 5, 6 ]
- Using reduce and recursion to achieve
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 native method
arr.flat(Infinity)The second parameter is the dimension of the multi-dimensional array, which can be set to Infinity, which can flatten the infinite multi-dimensional array. When it is set to 2, it means that the arr will be flattened to 2 layers
For example, arr = [ 1, [ 2, 3], [4, [ 5, [ 6 ] ] ] ]
Then arr.flat(2) is [ 1, 2, 3, 4, 5, [ 6 ] ]
边栏推荐
- A line of code counts the number of occurrences of the specified string in the text
- MySQL——索引与事务
- Exercise equipment responsive pbootcms template class web site
- spark入门学习-3-SparkSQL数据抽象
- ToDesk企业版上新 | 十大新功能,让企业远控更安全、更便捷、更流畅
- 32. Do you know how Redis strings are implemented?
- Cube - studio deployment process
- Building a High-Performance Platform on AWS Using Presto and Alluxio to Support Real-Time Gaming Services
- 【Review of Live Streaming】Synthesis MindSpore Usability SIG2022 First Half Review Summary
- Some excellent blog recommendations for Qt event learning reference
猜你喜欢
随机推荐
vulnhub-DC-3 drone penetration record
【OAuth2】十八、OIDC的认识应用
leetcode: 322.零钱兑换
spark入门学习-3-SparkSQL数据抽象
leetcode: 455. 分发饼干
07查询表达式 及 page分页、order 排序《ThinkPHP6 入门到电商实战》
Mini Program Optimization Practice
开发如何尽可能的避免BUG
【opencv】opencv开发包简介
机器学习笔记:学习率预热 warmup
Awk syntax-03-awk expressions (if statements, while loops, for loops), execute shell commands in awk
MySQL——索引与事务
强网杯 2019-随便注 (堆叠注入)
vulnhub-DC-3靶机渗透记录
报错:[Intervention] Unable to preventDefault inside passive event listener due to target ...
The use of mmedicting get_flops. Py
奇怪的魔法(组合数)
A line of code counts the number of occurrences of the specified string in the text
实现Express中间件原理
The type of block in the database buffer cache









