【金秋打卡】第4天 8-3 数组扩展:Array.prototype.flat(),Array.prototype.flatMap()

2022/10/28 4:24:52

本文主要是介绍【金秋打卡】第4天 8-3 数组扩展:Array.prototype.flat(),Array.prototype.flatMap(),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

课程名称:JavaScript ES(6-11)全版本语法 每个前端都需要的基础课

课程章节:8-3 数组扩展:Array.prototype.flat(),Array.prototype.flatMap()

课程讲师: 谢成

课程内容:
8-3 数组扩展:Array.prototype.flat(),Array.prototype.flatMap()

课程收获:

flat() 方法会按照一个可指定的深度递归遍历数组,并将所有元素与遍历到的子数组中的元素合并为一个新数组返回。flat() 方法会移除数组中的空项:

flatMap() 方法首先使用映射函数映射每个元素,然后将结果压缩成一个新数组。它与 map 连着深度值为 1 的 flat 几乎相同,但 flatMap 通常在合并成一种方法的效率稍微高一些。

const arr = [1, 2, 3, [4, 5, 6, [7, 8, 9, [10, 11, 12]]]]
console.log(arr.flat())
console.log(arr.flat().flat())
console.log(arr.flat().flat().flat())
console.log(arr.flat(3))
console.log(arr.flat(Infinity))

const arr = [1, 2, 3, 4, 5]
const res = arr.map(x => x + 1)
const res = arr.map(x => [x + 1]).flat()
const res = arr.flatMap(x => [x + 1])
console.log(res)

谢谢老师,讲的非常细致,很容易懂。这一节学的是数组扩展:Array.prototype.flat(),Array.prototype.flatMap(),给以后的学习打下了基础。
原来ES6-11能有这么多种性质,以及对ES6-11有了新的认识,期待后边的学习

图片描述
图片描述
图片描述
图片描述



这篇关于【金秋打卡】第4天 8-3 数组扩展:Array.prototype.flat(),Array.prototype.flatMap()的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程