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. 给内部类对象数组属性赋值时报错:Exception in thread "main" java.lang.NullPointerException

    前言 1255: 打怪升级(Java),写这个题目程序的时候,控制台提示如下错误: Exception in thread "main" java.lang.NullPointer ...

  2. Python开发【第三章】:函数介绍

    一. 函数介绍 1.函数是什么? 在学习函数之前,一直遵循面向过程编程,即根据业务逻辑从上到下实现功能,其往往用一长段代码来实现指定功能,开发过程中最常见的操作就是粘贴复制,也就是将之前实现的代码块复 ...

  3. java-TheadPoolExecutor

    Executor的两极调度模型 第一级:java多线程程序把应用分解为若干个任务,然后使用用户级的调度器(Executor框架)将这些任务映射为固定数量的线程: 第二级:操作系统内核将这些线程映射到处 ...

  4. IDEA报错: Clone failed: Authentication failed for 'http://10.70.XXXXXXXXXXXXXXXXX'

    今天从git上导入公司的项目,总是报错Clone failed: Authentication failed for 'http://10.70.XXXXXXXXXXXXXX' 在网上百度了一下,大致 ...

  5. Elasticsearch 介绍及应用

    Elasticsearch简单介绍 Elasticsearch (ES)是一个基于Lucene构建的开源.分布式.RESTful 接口全文搜索引擎.Elasticsearch 还是一个分布式文档数据库 ...

  6. 【转载】springboot启动报错(Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWe)

    SpringBoot启动时的异常信息如下: 1 "C:\Program Files\Java\jdk1.8.0_161\bin\java" ......... com.fangxi ...

  7. Linux 配置jdk vim和 Linux 基本操作

    1下载jdk tar.gz 安装包(http://www.oracle.com/) 注意安装机器的Linux 是x86(32位)还是x64(64位)的 2使用tar -zxvf jdk.tar.gz解 ...

  8. 简单SQL注入试探、二

    DVWA——简单SQL注入小记 今天我们来记录简单的盲注过程 简单的SQL injection(blind) Level:low 登陆后选择SQL Injection(Blind) 能看到这样的界面 ...

  9. python matplotlib以日期为x轴作图

    from datetime import datetime, date, timedelta import matplotlib.pyplot as plt import tushare as ts ...

  10. Java中程序、进程、线程的区别。

    程序.进程.线程的区别. 程序(program):是一个指令的集合.程序不能独立执行,只有被加载到内存中,系统为他分配资源后才能执行. 进程(process):一个执行中的程序称为进程. 进程是系统分 ...