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 ) }