当前位置:网站首页>es10小计flat和flatMap
es10小计flat和flatMap
2022-07-24 05:17:00 【LuciferDawnC】
1.flat()
数组扁平化,或者说数组降维!
通过
let arr = [1,[2,3],5,[4,[6,7]]];
let arr2 = arr.flat(); // 将数组降维为[1,2,3,5,4[6,7]]
简单说就是将数组中的数组拆解插入到当前它所在的位置
flat()可以带参数默认为1也就是说
let arr2 = arr.flat(); 等价为let arr2 = arr.flat(1);
参数的做用是用来识别降维几层的多维数组为2时次数组中包含的第三层子数组[6,7]也会解析出来为[1, 2, 3, 5, 4, 6, 7]
2.flatMap()
此方法和flat一样会降维数组 不过此方法只会降维一次 并可以对数组中的元素进行迭代通过return返回 (不会改变原数组 需要使用一个变量容器承接)
let arr = [1,[2,3],5,[4,[6,7]]];
let arr2 = arr.flatMap(item => {
// 这里可以运算
// return出去的数会生成一个新数组
return item
}); // 将数组降维为[1,2,3,5,4[6,7]]
console.log(arr2, arr)
// [1, 2, 3, 5, 4, Array(2)] [1,[2,3],5,[4,[6,7]]]
边栏推荐
- C语言进阶篇 七.程序的编译和预处理
- special effects - 鼠标移动,出现泡泡拖尾
- 函数_概括
- Collation of commonly used Anaconda commands
- 作用域与作用域链
- anaconda常用命令的整理
- AttributeError: ‘NoneType‘ object has no attribute ‘shape‘
- String的字符串常量池和intern()详解
- 用C语言写出三子棋
- Relationship between sample and population in Statistics: sample success ratio + central limit theorem (sample mean)
猜你喜欢
随机推荐
【Pytorch】Dataset_DataLoader
Pure white tutorial using Druid database connection pool in idea
C2 random generation function seed, numpy. Random. Seed(), TF. Random. Set_ Seed Learning + reprint and sorting
C语言从入门到入土(一)
T 6-10
6.在屏幕上绘制一条贝塞尔曲线和一个贝塞尔曲面
Redis的使用
Basic usage of analog Addition & structure
在屏幕上绘制一个运动的茶壶,茶壶先慢慢向屏幕里面移动,越变越小,越变越模糊;然后慢慢变大,越变越清晰,一直往返重复。为场景添加光照,材质和雾效果。通过键盘’a’’s’’d’来选择不同的雾效模式
day(0~6)代表每月第一天起始位置,stop代表每月天数,每天之间空两个空格。输入不同的day和stop,输出每月日历的样子。假设day为2,stop为31,则输出样式为
anaconda常用命令的整理
reflex
special effects - 星空宇宙背景特效
用C语言写出三子棋
用双向链表实现栈(C)
赶紧进来!!轻松掌握C语言“顺序”、“分支”、“循环”三大结构
Scikit learn notes
T 1-5
yocs_velocity_smoother源码编译
C语言入门篇 概述









