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. sql server 2012 链接服务器不能链接sql server 2000的解决方案 ,

    本数据源来自 https://www.kafan.cn/edu/922556.html  目的为了备忘 把原来的sql server 2005直接装成了2012,然后在建立链接服务器链接一台sql s ...

  2. java--demo之图书馆管理系统

    aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQEAkACQAAD/4RDQRXhpZgAATU0AKgAAAAgABAE7AAIAAAADTXkAAIdpAAQAA ...

  3. MySQL之创建用户和授权

    一 权限管理 我们知道我们的最高权限管理者是root用户,它拥有着最高的权限操作.包括select.update.delete.update.grant等操作.那么一般情况在公司之后DBA工程师会创建 ...

  4. 十一、三星平台framebuffer驱动

    和总线设备驱动模型类似,framebuffer分为核心层.驱动层和设备层. 核心层:就是上一章分析的fbmem.c文件 驱动层(控制器层):一般由芯片原厂提供,实现了LCD控制器通用的操作接口和配置接 ...

  5. SAS学习笔记27 卡方检验

    卡方检验(chi-square test)是英国统计学家Pearson提出的一种主要用于分析分类变量数据的假设检验方法,该方法主要目的是推断两个或多个总体率或构成比之间有无差别. 卡方分布界值表的依据 ...

  6. JS 04 Date_Math_String_Object

    Date <script> //1.Date对象 var d1 = new Date(); //Thu May 02 2019 14:27:19 GMT+0800 (中国标准时间) con ...

  7. MongoDB增删改

    一.数据库操作 显示现有的数据库,命令:show dbs 或者 databases; 示当前使用的数据库,命令:db 切换当前使用的数据库,命令:use 数据库名称 删除数据库,命令:db.dropD ...

  8. 题解-PKUWC2018 猎人杀

    Problem loj2541 题意概要:给定 \(n\) 个人的倒霉度 \(\{w_i\}\),每回合会有一个人死亡,每个人这回合死亡的概率为 自己的倒霉度/目前所有存活玩家的倒霉度之和,求第 \( ...

  9. 怎样理解this

    JavaScript里的this, Python里的self, 其实都是一个东西, 它的存在跟构造函数 / 类这种是分不开的, 当然, 也可以在其他场合下使用, 他的意义很多, 但最共通的一个特点是: ...

  10. c#基础知识梳理(四)

    上期回顾 - https://www.cnblogs.com/liu-jinxin/p/10826971.html 一.类 当你定义一个类时,你定义了一个数据类型的蓝图.这实际上并没有定义任何的数据, ...