flat()
深度递归遍历数组,并将所有元素与遍历到的子数组中的元素合并为一个新数组返回
用法
const newArr = arr.flat(num)
const newArr2 = [1, 2, 3, ['a']].flat()//[1, 2, 3, a]const newArr = [1, 2, 3, ['a', 'b', 'c', ['Aa']]].flat(2)//[1,2,3,"a","b","c","Aa]
flatMap()
Map():map 可以遍历数组处理数据,并返回新的数组
flatMap(): 只能展开一层数组
const arr = [[1], [2], [3]]let newArr = arr.flatMap(item => [item*3])console.log(newArr); // [3, 6, 9]let newArr2 = arr.map(item => [item*3])console.log(newArr2); // [[3], [6], [9]]