[ES2019] Use JavaScript ES2019 flatMap to Map and Filter an Array
ES2019 introduces the Array.prototype.flatMap method. In this lesson, we'll investigate a common use case for mapping and filtering an array in a single iteration. We'll then see how to do this using Array.prototype.reduce, and then refactor the code to do the same thing using the ES2019 .flatMap method.
Let's recap what we know about .filter and .map in JS.
For .filter:
We can choose to include or exclude one element from the original array, but we cannot apply transform function to the item.
For .map:
We can apply transform function to the element but we cannot exclude the element from the array.
const data = [{a: , b: , c: }, {a: , b: , c: }, {a: , b: , c: }]
function getABWhereCLargerThan100 (data) {
return data.flatMap(o => o.c >= ? [o.a, o.b]: [])
}
console.log(getABWhereCLargerThan100(data)); //[1, 2, 13, 22]
If inside .flatMap we return [], it is the same as filtering current element from the array.
So if we want to apply transform function and also at the same time filter out some elements, we can use .flatMap from ES2019:
[ES2019] Use JavaScript ES2019 flatMap to Map and Filter an Array的更多相关文章
- JavaScript中foreach、map、filter、find、every、some的用法
foreach:只是循环数组中的每一项,没有返回值 如: var arr = [2,3,3,4,5,6]; arr.foreach(function(item,index,array){ dosom ...
- JavaScript高阶函数 map reduce filter sort
本文是笔者在看廖雪峰老师JavaScript教程时的个人总结 高阶函数 一个函数就接收另一个函数作为参数,这种函数就称之为高阶函数 1.高阶函数之map: ...
- (八)map,filter,flatMap算子-Java&Python版Spark
map,filter,flatMap算子 视频教程: 1.优酷 2.YouTube 1.map map是将源JavaRDD的一个一个元素的传入call方法,并经过算法后一个一个的返回从而生成一个新的J ...
- Spark RDD/Core 编程 API入门系列 之rdd案例(map、filter、flatMap、groupByKey、reduceByKey、join、cogroupy等)(四)
声明: 大数据中,最重要的算子操作是:join !!! 典型的transformation和action val nums = sc.parallelize(1 to 10) //根据集合创建RDD ...
- 百度地图JavaScript API经纬度查询-MAP
百度地图JavaScript API经纬度查询-MAP-ABCDEFGHIJKMHNOPQRSTUVWXYZ: 搜索:<input type="text" size=&quo ...
- Spark入门1(以WordCount为例讲解flatmap和map之间的区别)
package com.test import org.apache.spark.{SparkConf, SparkContext} object WordCount { def main(args: ...
- spark的flatMap和map区别
map()是将函数用于RDD中的每个元素,将返回值构成新的RDD. flatmap()是将函数应用于RDD中的每个元素,将返回的迭代器的所有内容构成新的RDD,这样就得到了一个由各列表中的元素组成的R ...
- Flatmap 和map 区别
map将函数作用到数据集的每一个元素上,生成一个新的分布式的数据集(RDD)返回 map函数的源码: def map(self, f, preservesPartitioning=False): ...
- [Javascript] Chaining the Array map and filter methods
Both map and filter do not modify the array. Instead they return a new array of the results. Because ...
随机推荐
- 1262: 谁不爱打牌(Java)
WUSTOJ 1262: 谁不爱打牌 转自 断-肠-人的博客 Java代码在文末 Description BobLee最近在复习考研,但是他也喜欢打牌(有谁不爱玩牌呢?).但是作为一名ACMER,斗地 ...
- 一文搞懂嵌入式uboot、kernel、文件系统的关系
总览: 在linux系统软件架构可以分为4个层次(从低到高分别为): 1.引导加载程序 引导加载程序(Bootloader)是固化在硬件Flash中的一段引导代码,用于完成硬件的一 ...
- 将物理机系统转为虚拟机系统 p2v
ref : https://blog.csdn.net/gsls200808/article/details/77932713 背景: 在公司有台机子主要负责某产品的升级与维护,出于各种原因,该产品需 ...
- Go学习路径--相关基础
现在开始接触Go一段时间了,基本路径就是看基础学习材料,开始写项目,有问题找解决问题的方法.这里记录一下学习过程. go相关文章 Golang适合高并发场景的原因分析 go build 不同系统下的可 ...
- MacBook使用HHKB键盘设置
问题: macbook上使用hhkb的时候,很纠结档位4要不要开,开启4号DIP开关后,虽然会使HHKB键盘上减少一个“◇(Win键)”键,但是会在键盘左侧多出一个“FN”键.多出来的左“FN”键,不 ...
- python03-break、continue、for循环、数据bytes类型、字符串与字节的关系、变量指向与深浅拷贝、set集合、文件操作
目录: 1.break.continue 2.for循环 3.数据bytes类型 4.字符串与字节的关系 5.变量指向与深浅拷贝 6.set集合 7.文件操作 一.break.continue bre ...
- CAS 5.x搭建常见问题系列(3).Failure to find org.apereo.cas:cas-server-support-pm-jdbc:jar:5.1.9
错误内容 cas overlay的pom.xml增加了cas-server-support-pm-jdbc.jary依赖后, 打包(mvn package)出现如下的报错 D:\casoverlay\ ...
- (一)CXF之发布第一个WebService服务
一.CXF入门 1.1 前提 Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS .这些 ...
- 基于【 centos7】二 || 系统时间与网络时间同步
# date // 查看系统时间 #hwclock // 查看硬件时间 # yum -y install ntp ntpdate 安装ntpdate工具 # ntpdate cn.pool.ntp.o ...
- Kmalloc可以申请的最大内存
Kmalloc申请的最大内存 以前虽然读过源码,但是对于它的申请上限确实没注意过.下面分析下,下面是kmalloc的源码. 可以看出,如果想知道kmalloc能申请的范围,需要跳转到<linux ...