JS手写flat函数
flat() 方法会按照一个可指定的深度递归遍历数组,并将所有元素与遍历到的子数组中的元素合并为一个新数组返回。const arr = [1, 2, [3, 4, [5, 6, [7]]], 8];console.log(arr.flat(Infinity)); //[1,2,3,4,5,6,7,8]console.log(arr.flat(2)); //[1,2,3,4,5,6,[7],8]手写flat函数:function flat(arr,n) { let times = 0; //利