博客地址:https://ainyi.com/19
问题引入
请写一个 flat 方法,实现扁平化嵌套数组
对于 [ [], [], [], ...]...数组里嵌套数组,有个需求:将里面的数组元素都放到外层数组,变成 , , , ......3, 4, 5, 6, 7, 8, 9;
变成:arr = 1, 2, 3, 4, 5, 6, 7, 8, 9;
倒是有几种方法:
// 模拟:执行内含 10000 子数组 + 子数组有 13 个元素的数组...).split(',').map(item => +item);
console.log(new Date() - nowTime, 'toString、split、map');
// 全部数字类型的:...,(数组元素较短时推荐,写法简便)
// 用时:5.7s
newArr = [];
nowTime = new Date();
// 默认指定第一次的prev为[]
newArr = arr.reduce