数组扁平化的方法

1 console.log(
2   [1, [2, 3, [4, 5]]]
3     .toString()
4     .split(",")
5     .map((item) => +item)
6 ); 
7 //[ 1, 2, 3, 4, 5 ]

通过
[1, [2, 3, [4, 5]]].flat(Infinity)

实质:
Array.prototype.flat = function() { return this.toString().split(',').map(item => +item ) }

 

posted @ 2021-09-04 16:21  一个动态类型的幽灵  阅读(24)  评论(0编辑  收藏  举报