博客
关于我
数组方法的扩展flat()和flatMap()
阅读量:586 次
发布时间:2019-03-11

本文共 587 字,大约阅读时间需要 1 分钟。

  • flat()将多维数组转化为低维数组
const arr = [1,2,3,4,[5,6]]console.log(arr.flat())        //[1, 2, 3, 4, 5, 6]const arr1 = [1,2,3,4,[5,6,[7,8,9]]]console.log(arr1.flat())       //[1, 2, 3, 4, 5, 6, [7,8,9]] //想将多维数组转成一维数组,则需要参数console.log(arr1.flat(2))      //[1, 2, 3, 4, 5, 6, 7, 8, 9]
  • flatMap()
const arr = [1,2,3,4,5]const result = arr.map(item => [item * 100])    console.log(result)     //返回一个二维数组,如下图

在这里插入图片描述

flatMap()方法将返回的这个二维数组转成一维数组

//flatMapconst arr = [1,2,3,4,5]const result = arr.flatMap(item => [item * 100])    console.log(result)      //[100, 200, 300, 400, 500]

flat()和flatMap()都是ES10中的新特性

转载地址:http://leftz.baihongyu.com/

你可能感兴趣的文章
MySQL主从架构与读写分离实战
查看>>
MySQL主从篇:死磕主从复制中数据同步原理与优化
查看>>
mysql主从配置
查看>>
MySQL之2003-Can‘t connect to MySQL server on ‘localhost‘(10038)的解决办法
查看>>
MySQL之CRUD
查看>>
MySQL之DML
查看>>
Mysql之IN 和 Exists 用法
查看>>
MYSQL之REPLACE INTO和INSERT … ON DUPLICATE KEY UPDATE用法
查看>>
MySQL之SQL语句优化步骤
查看>>
MYSQL之union和order by分析([Err] 1221 - Incorrect usage of UNION and ORDER BY)
查看>>
Mysql之主从复制
查看>>
MySQL之函数
查看>>
mysql之分组查询GROUP BY,HAVING
查看>>
mysql之分页查询
查看>>
Mysql之备份与恢复
查看>>
mysql之子查询
查看>>
MySQL之字符串函数
查看>>
mysql之常见函数
查看>>
Mysql之性能优化--索引的使用
查看>>
mysql之旅【第一篇】
查看>>