当前位置:网站首页>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 ] ]
边栏推荐
猜你喜欢

The storage principle of NorFlash

手把手教你手撕SPF生成树(OSPF区域内防环)

32. 你知道Redis的字符串是怎么实现的吗?

The live broadcast of agricultural products continues to heat up, Economic Daily: Don’t forget quality when rushing

数据在内存如何分布的?

A line of code counts the number of occurrences of the specified string in the text

分类、目标检测、语义分割、实例分割的区别

机器学习笔记:学习率预热 warmup

强网杯 2019-随便注 (堆叠注入)

08 获取器 withAttr、多连缀、whereRaw、事务、数据集《ThinkPHP6 入门到电商实战》
随机推荐
The sword refers to Offer 18. Delete the node of the linked list
机器学习笔记:学习率预热 warmup
08 获取器 withAttr、多连缀、whereRaw、事务、数据集《ThinkPHP6 入门到电商实战》
torch.view() function usage
[Code Analysis] Graph small sample anomaly detection method: GDN: Few-shot Network Anomaly Detection via Cross-network Meta-learning
单主机docker 搭建 redis-cluster
剑指 Offer 17. 打印从1到最大的n位数
Inside outside l think MindSpore AI framework, heavy industry gathering, huawei big extraordinary path of the model
报错:[Intervention] Unable to preventDefault inside passive event listener due to target ...
2022-08-07 mysql/stonedb slow SQL-subquery-semi-join
内修昇思MindSpore AI框架,外重行业汇聚,华为大模型的不平凡之路
【直播回顾】昇思MindSpore易用性SIG2022上半年回顾总结
Research on Blind Recognition of Digital Modulated Signal Based on MindSpore Framework
C语言 扫雷
OLTP和OLAP问题的个人总结
实现Express中间件原理
The type of block in the database buffer cache
reduce具体使用以及使用reduce,toString,flat进行数组降维
Voice identification software
高薪程序员&面试题精讲系列134之微服务网关有哪些限流算法?如何实现限流?