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的更多相关文章

  1. JavaScript中foreach、map、filter、find、every、some的用法

    foreach:只是循环数组中的每一项,没有返回值 如:  var arr = [2,3,3,4,5,6]; arr.foreach(function(item,index,array){ dosom ...

  2. JavaScript高阶函数 map reduce filter sort

    本文是笔者在看廖雪峰老师JavaScript教程时的个人总结 高阶函数            一个函数就接收另一个函数作为参数,这种函数就称之为高阶函数          1.高阶函数之map:   ...

  3. (八)map,filter,flatMap算子-Java&Python版Spark

    map,filter,flatMap算子 视频教程: 1.优酷 2.YouTube 1.map map是将源JavaRDD的一个一个元素的传入call方法,并经过算法后一个一个的返回从而生成一个新的J ...

  4. Spark RDD/Core 编程 API入门系列 之rdd案例(map、filter、flatMap、groupByKey、reduceByKey、join、cogroupy等)(四)

    声明: 大数据中,最重要的算子操作是:join  !!! 典型的transformation和action val nums = sc.parallelize(1 to 10) //根据集合创建RDD ...

  5. 百度地图JavaScript API经纬度查询-MAP

    百度地图JavaScript API经纬度查询-MAP-ABCDEFGHIJKMHNOPQRSTUVWXYZ: 搜索:<input type="text" size=&quo ...

  6. Spark入门1(以WordCount为例讲解flatmap和map之间的区别)

    package com.test import org.apache.spark.{SparkConf, SparkContext} object WordCount { def main(args: ...

  7. spark的flatMap和map区别

    map()是将函数用于RDD中的每个元素,将返回值构成新的RDD. flatmap()是将函数应用于RDD中的每个元素,将返回的迭代器的所有内容构成新的RDD,这样就得到了一个由各列表中的元素组成的R ...

  8. Flatmap 和map 区别

    map将函数作用到数据集的每一个元素上,生成一个新的分布式的数据集(RDD)返回 map函数的源码:   def map(self, f, preservesPartitioning=False): ...

  9. [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 ...

随机推荐

  1. Jmeter 跨线程组传递参数 之两种方法(转)

    终于搞定了Jmeter跨线程组之间传递参数,这样就不用每次发送请求B之前,都需要同时发送一下登录接口(因为同一个线程组下的请求是同时发送的),只需要发送一次登录请求,请求B直接用登录请求的参数即可,直 ...

  2. SSH协议(1)-工作原理及过程

    转载. https://blog.csdn.net/cjx529377/article/details/77659199 SSH全称是Secure Shell,SSH协议是基于应用层的协议,为远程登录 ...

  3. 扩散:Apache2放开virtualhost,wamp启动apache服务失败

    原文链接:https://blog.csdn.net/weixin_45688623/article/details/101423164 CSDN写过过程了,有点长,这里不赘述了,只写最后我设置的结果 ...

  4. Power BI学习

    常见用途: 1.连接数据 2.转换和清洗该数据,以创建数据模型 3.创建视觉对象,如提供数据的可视化表示形式的图表或图形 4.在一个或者多个报表页上创建作为视觉对象集合的报表 5.使用Power BI ...

  5. 20191011-构建我们公司自己的自动化接口测试框架-Util的读取excel常用方法模块

    包括获取excel的sheet名字,设定excel的sheet,读excel,写excel等常规操作. from openpyxl import Workbook from openpyxl impo ...

  6. STM32与ARM代码执行过程

    内存分配 1.ARM(JZ2440) 启动方式: 1)nor启动 注:1.bootloader烧在norflash的0地址 2.将bootloader从norflash中复制到SDRAM中的链接地址( ...

  7. DMA存储器到外设代码讲解

    实验目的: bsp_dma_mtp.h #ifndef __BSP_DMA_MTP_H #define __BSP_DMA_MTP_H #include "stm32f10x.h" ...

  8. MogileFS与spring结合

    一.通过Maven添加MogileFS的Java客户端驱动包 <dependency> <groupId>fm.last</groupId> <artifac ...

  9. 为什么用JS取不到cookie的值?解决方法如下!

    注意:cookie是基于域名来储存的.要放到测试服务器上或者本地localhost服务器上才会生效.cookie具有不同域名下储存不可共享的特性.单纯的本地一个html页面打开是无效的. 明明在浏览中 ...

  10. 关于OI中的各种数学

    学到后面数学越来越多了,感觉好难啊,开个博客专门记录一下数学相关的东西 因为反正也没人看,所以主要还是给自己看的 一些符号: 数论函数的卷积:$\ast$,$ h = f \ast g$ 则 $h(n ...