当前位置:网站首页>(JS)数组排平(flat)
(JS)数组排平(flat)
2022-06-29 09:57:00 【不愿透露姓名的余菜鸟】
- 如果只有一层嵌套数组情况
Array.prototype.concat.apply([], arr)
或
[].concat(arr)
或
Array.prototype.concat.call([], arr[0],arr[1]...)
- 如果有多层嵌套
function flat(arr) {
//判断是否有多层数组
const isDeep = arr.some(item => {
return item instanceof Array;
});
//如果没有多层数组,则返回arr
if (!isDeep) {
return arr;
}
const res = Array.prototype.concat.apply([], arr);
return flat(res)
}
边栏推荐
- 【评论送书】适合初学者的 6 个有趣的 R 语言项目
- Atomic explanation of AQS
- 【C语言进阶】字符串和内存函数(一)
- Does anyone encounter this problem when flinkcdc synchronizes MySQL?
- 全面理解Volatile关键字
- Ora-01950 does not have permission on tablespace
- Does your project need automated testing?
- 认不出原来的模样
- Given the values of two integer variables, the contents of the two values are exchanged (C language)
- 【C语言进阶】自定义类型
猜你喜欢

BUUCTF--reverse1

免费送书啦!畅销书《 OpenCV图像处理入门与实践》一本全搞定

【C语言进阶】字符串和内存函数(一)

Fully understand the MESI cache consistency protocol

【C语言进阶】自定义类型

软件测试模型(V模型和W模型)

WinForm uses zxing to generate QR code

Comprehensive understanding of synchronized

悬赏平台并没有WEB端开发,在原生开发和混合开发中哪种合适?

Analysis of reentrantlock source code of AQS
随机推荐
细粒度识别、分类、检索数据集整理
小米手机-解BL锁+开ROOT权限
反CSRF爆破的三种姿势
学习通否认 QQ 号被盗与其有关:已报案;iPhone 14 量产工作就绪:四款齐发;简洁优雅的软件早已是明日黄花|极客头条...
悬赏平台并没有WEB端开发,在原生开发和混合开发中哪种合适?
Dormitory maintenance management system based on stm32+rfid design
最后的 48 小时!云 XR 专题赛邀你一起绽放精彩,我们赛场见!
USB转RS485串口电路设计「建议收藏」
Hystrix fuse: Service fusing and service degradation
Fully understand the volatile keyword
C language printf family
MySQL查询时如何找出错误格式的手机号
SQL Server 数据库的几种简单查询
8年打磨,《游戏设计梦工厂》发布史诗级更新!
Numeric Keypad
Mongodb tutorial Chapter 02 mongodb installation
智能组卷系统设计
Does your project need automated testing?
Highly paid programmers & interview questions: how to ensure the data consistency between redis cache and database in series 117?
C#MDI打开子窗体去掉自动生成的菜单栏