js 数组展开之 flat()
语法:
letnewArray = arrayObject.flat([deepth])
deept 参数指定该方法使数组结构平坦的深度,它默认为1
示例1:
const arr1=[1,2,3,4,[5,6]]
const arr=arr1.flat()
console.log(arr) //[1, 2, 3, 4, 5, 6]
示例2:
const arr1=[1,2,3,4,[5,6,[7,8]]]
const arr=arr1.flat(2)
console.log(arr) // [1, 2, 3, 4, 5, 6, 7, 8]
示例3:
const arr1=[1,2,3,4,[5,6,[7,8,[9,10]]]]
const arr=arr1.flat(Infinity) //Infinity 为全部展开
console.log(arr) //[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]