<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>flat 与 flatMap</title> </head> <body> <script> // flat 平 // 将多维数组转化为低维数组 const arr1 = [1, 2, 3, 4, [5, 6]]; console.log(arr1.flat()) // [1, 2, 3, 4, 5, 6] const arr2 = [1, 2, 3, 4, [5, 6, [7, 8, 9]]]; // 参数为深度 是一个数字 console.log(arr2.flat(2)); // [1, 2, 3, 4, 5, 6, 7, 8, 9] const arr3 = [1, 2, 3, 4, [5, 6, [7, [8, 9]]]]; console.log(arr3.flat(Infinity)); // [1, 2, 3, 4, 5, 6, 7, 8, 9] // flatMap:结合了flat、map两步操作。 const arr4 = [1, 2, 3, 4]; const result = arr4.flatMap(item => [item * 10]); console.log(result); // [10, 20, 30, 40] </script> </body> </html>
542 Array.prototype.flat 与 flatMap
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
上一篇:RTT学习之软件包
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
typescript中使用Array定义的一维数组成员增删改查操作
本文小结typescript中使用Array定义的一维数组成员增删改查操作
数组 增删改查 数组方法 typescipt Array -
Array.prototype.flat() (扁平化数组 )
扁平化数组 Array.prototype.flat方法使用方法总结代码示例使用方法总结1. Array.prototype.flat() 用于将嵌套的数组“拉平”,变成一维的数组。该方法返回一个新数组,对原数据没有影响。2. 不传参数时,默认“拉平”一层,可以传入一个整数,表示想要“拉平”的层数。3. 传入 参数<=0 的整数将返回原数组,不“拉平”。4. Infinity 关键字作为参数时,无论多少层嵌套,都会转为一维数组。5. 如果原数组有空位,Array.prototype.fla
javascript vue.js es6 array html5 -
【ES6+】012-ES10新特性:Object.fromEntries、trimStart、trimEnd、Array.prototype.flat、flatMap、Symbol.prototype
一、功能概述
js javascript 字符串 html 二维数组 -
JavaScript 数组展平方法: flat() 和 flatMap()
从 ES2019 中开始引入了一种扁平化数组的新方法,可以展平任何深度的数组,这个方法就是 flat() 。而说起 flat() 的方法,就不得不顺便说下 flatMap()
JavaScript ECMAScript 6 数组 可选参数 回调函数 -
prototype的解读之Array
整理部分prototype的Array相关的api 1、first() 原api的用途: Returns the first item inhang","yao...
first last prototye Array 数组 -
flat arrayarray flat 数组 扁平化 数据 升序