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. (三)Servlet 知识点总结(来自那些年的笔记)

    (史上最全知识汇总)转载请贴上原文链接! 作者:淮左白衣 写于 2018年4月15日20:14:55 如果,碰巧你打开了本篇博客,相信我,你想要的servlet知识,这里应该都能找到!! 目录 (史上 ...

  2. WUST 设计模式 实验九 观察者模式的应用

    实验九 观察者模式的应用 一.实验目的 掌握外观模式(Observer)的特点: 分析具体问题,使用外观模式进行设计. 二.实验内容和要求   网上商店中如果商品(product)在名称(name). ...

  3. Yii2 基于header 实现接口版本控制

    Yii2 官方给出的方案是基于url的版本控制,但是我们的versoin放在header里面,需要通过header来进行版本控制,实现如下: 首先在基类中实现actions,actions是针对con ...

  4. MyBatis_02 框架

    今日内容 动态SQL语句 Xml方式 注解方式 MyBatis的缓存 MyBatis的关联查询 MyBatis逆向工程 动态SQL语句 动态SQL是什么 就是相对与固定SQL.就是通过传入的参数不一样 ...

  5. 剑指offer(9)——用两个栈实现队列

    题目: 用两个栈实现一个队列.队列的声明如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能. 思路: 首先定义两个栈stack1. ...

  6. JDBC 学习复习7 学习 Apache 开源DBCP 数据源

    DBCP(DataBase connection pool),数据库连接池.是 apache 上的一个 java 连接池项目,也是 tomcat 使用的连接池组件.单独使用dbcp需要2个包:comm ...

  7. css布局笔记

    1.display   block块级元素(div.p等) inline 行内元素(a.span等) 常见的例子:把li修改成inline ,制作成水平菜单 2.max-width 来适应不同浏览器窗 ...

  8. 2019年Java后端工程师常见面试题和感想

    来新公司有5个月了,从第二个月开始就参与公司后端工程师的面试工作了,包括校招在内,面试超过100个(包括40个校招的终面)应聘者了,应聘者中有超过10年的技术经理,有6年以上的高级开发,有3到5年的中 ...

  9. ubuntu打开终端

    虚拟终端 Ctrl+Alt+T组合键 实际终端 Ctrl+Alt+F1 ~ F6组合键 可以在登录界面直接切换 退出终端 Ctrl+Alt+F7组合键

  10. KVM之virsh管理Storage pool

    创建基于文件夹的存储池 基于文件夹的存储池: [root@ubuntu01 ~]# mkdir /data/vm_pool [root@ubuntu01 ~]# virsh pool-create-a ...