[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 ...
随机推荐
- MySQL和Oracle的区别与不同
一.mysql与Oracle的相同点: 1.都是关系型数据库管理系统(MySQL开放源码) 2.都是目前很流行的数据库(Oracle以分布式为核心): 二.MySQL.Oracle各自特点: 1.Or ...
- 去掉右键Open Folderas Intellij IDEA Project
解决: WIN+R键打开运行,输入regedit 打开注册表 在地址栏输入: 计算机\HKEY_CLASSES_ROOT\Directory\Background\shell\Powershell 然 ...
- ARTS第九周打卡
Algorithm : 做一个 leetcode 的算法题 /* 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. ...
- 正则与re模块
一.正则表达式 在线测试工具 http://tool.chinaz.com/regex/ 1.字符组 在同一个位置可能出现的各种字符组成一个字符组,在正则表达中用[ ]表示 一个正则就是一条匹 ...
- scratch少儿编程第一季——07、人要衣装佛靠金装——外观模块
各位小伙伴大家好: 上期我们学习了如何设置背景,和设计一个小项目总结了动作模块. 本期开始我们学习外观模块下的指令. 首先我们来看看前面两个指令 第一个指令是在角色对象上出现一个对话框,显示角色所说的 ...
- redis字符串数据类型基本概念和应用场景
基本概念:1.string类型是redis能与键关联的最简单的数据类型,它是memcached当中仅有的数据类型.2.redis的key名称也是一个字符串,当我们使用字符串类型作为其对应的值时,我们可 ...
- Manacher算法+注释
Manacher算法是用来求一个字符串中最长回文串的算法. 考虑暴力求最长回文串的做法: 暴力枚举字符串中的所有字串判断是否回文,然后求最大值. 时间复杂度O(n^3),考虑优化. 我们从枚举所有字串 ...
- 第三代PacBio测序技术的测序原理和读长
针对PacBio单分子测序——第三代测序技术的测序原理和读长 DNA基因测序技术从上世纪70年代起,历经三代技术后,目前已发展成为一项相对成熟的生物产业.测序技术的应用也扩展到了生物.医学.制 ...
- javascript的装载和执行
前言 为什么要采用js来create一个script标签,设置src然后append到head中,而不是直接使用script标签,这样不是更简单点吗? javascript的装载和执行 首先,我想说一 ...
- layui下拉多选formSelects使用方法
下载formSelects-v4插件(引入formSelects-v4.css和formSelects-v4.js) 下载地址:https://fly.layui.com/extend/formSel ...