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 ) }
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 ) }
上一篇:深拷贝与浅拷贝的深度理解
下一篇:react为什么要使用 hook
1. 数组扁平化1.1 toString + split1.2 reduce1.3 join + split1.4 递归 + 循环1.5 扩展运算符1.6 flat()2. 对象扁平化
(1)join 方法 let colors = ['red',
js--数组扁平化
直接上代码
举报文章
请选择举报类型
补充说明
0/200
上传截图
格式支持JPEG/PNG/JPG,图片不超过1.9M