[Javascript] Advanced Reduce: Flatten, Flatmap and ReduceRight
Learn a few advanced reduction patterns: flatten allows you to merge a set of arrays into a single array, the dreaded flatmap allows you to convert an array of objects into an array of arrays which then get flattened, and reduceRight allows you to invert the order in which your reducer is applied to your input values.
Flatten
var data = [[1,2,3], [4,5,6], [7,8,9]];
var flatData = data.reduce( (acc, value) => {
return acc.concat(value);
}, []); console.log(flatData); //[1, 2, 3, 4, 5, 6, 7, 8, 9]
Flatmap
var input = [
{
title: "Batman Begins",
year: 2005,
cast: [
"Christian Bale",
"Michael Caine",
"Liam Neeson",
"Katie Holmes",
"Gary Oldman",
"Cillian Murphy"
]
},
{
title: "The Dark Knight",
year: 2008,
cast: [
"Christian Bale",
"Heath Ledger",
"Aaron Eckhart",
"Michael Caine",
"Maggie Gyllenhal",
"Gary Oldman",
"Morgan Freeman"
]
},
{
title: "The Dark Knight Rises",
year: 2012,
cast: [
"Christian Bale",
"Gary Oldman",
"Tom Hardy",
"Joseph Gordon-Levitt",
"Anne Hathaway",
"Marion Cotillard",
"Morgan Freeman",
"Michael Caine"
]
}
]; var flatMapInput = input.reduce((acc, value)=>{
value.cast.forEach((star)=>{
if(acc.indexOf(star) === -1){
acc.push(star);
};
}); return acc;
}, []); //["Christian Bale", "Michael Caine", "Liam Neeson", "Katie Holmes", "Gary Oldman", "Cillian Murphy", "Heath Ledger", "Aaron Eckhart", "Maggie Gyllenhal", "Morgan Freeman", "Tom Hardy", "Joseph Gordon-Levitt", "Anne Hathaway", "Marion Cotillard"]
ReduceRight
var countDown = [1,2,3,4,"5"];
var str = countDown.reduceRight((acc, value)=>{
return acc + value;
}, "");
console.log(str); //"54321"
[Javascript] Advanced Reduce: Flatten, Flatmap and ReduceRight的更多相关文章
- [Javascript] Advanced Reduce: Common Mistakes
Take away: Always check you ruturn the accumulator Always pass in the inital value var data = [" ...
- [Javascript] Advanced Reduce: Additional Reducer Arguments
Sometimes we need to turn arrays into new values in ways that can't be done purely by passing an acc ...
- [Javascript] Advanced Reduce: Composing Functions with Reduce
Learn how to use array reduction to create functional pipelines by composing arrays of functions. co ...
- JavaScript中reduce()方法
原文 http://aotu.io/notes/2016/04/15/2016-04-14-js-reduce/ JavaScript中reduce()方法不完全指南 reduce() 方法接收 ...
- JavaScript: Advanced
DOM 1. 节点 getElementsByName方法 <!DOCTYPE HTML> <html> <head> <script type=" ...
- [Javascript] Advanced Console Log Arguments
Get more mileage from your console output by going beyond mere string logging - log entire introspec ...
- [Javascript] Introducing Reduce: Common Patterns
Learn how two common array functions - map() and filter() - are syntactic sugar for reduce operation ...
- javascript之reduce()方法的使用
以前看到reduce方法,总是看得我头皮发麻,今天无意间又遇到他了,于是学习了下,接触之后,觉得这个方法还挺好用的,在很多地方都可以派上用场,比如,数组中元素求和.数组去重.求数组中的最大值或最小值等 ...
- JavaScript map reduce
23333333333333 map var s = []; for(let i=0;i<10;i++){ s.push(i); } function pow(x){ return x*x; } ...
随机推荐
- xmlns:android="http://schemas.android.com/apk/res/android" 是什么意思?
声明xml命名空间.xmlns意思为“xml namespace”.冒号后面是给这个引用起的别名.schemas是xml文档的两种约束文件其中的一种,规定了xml中有哪些元素(标签).元素有哪些属性及 ...
- c# 使用递归 循环遍历导航树结构 并解析
1.数据书库结构 1 家用电器 0 一级菜单 2 手机.数码.京东通信 0 一级菜单 3 电脑.办公 0 一级菜单 4 家具.家居.厨房 0 一级菜单 5 男装.女装.童装.内衣 0 一级菜单 6 个 ...
- idea 多模块项目依赖父工程class找不到问题
比如,我们有这么个过程,项目结构如下: a --b --c a是总结点,b是子节点,c是父节点 b依赖父节点class,通过maven构建时通常我们会在子节点中添加父节点依赖,如: <depen ...
- hibernate_validator_07
一.校验组序列 默认情况下,约束的验证是没有一定的顺序的,不管他们是属于哪个认证组的.但是在有些环境中,我们控制这些约束验证的顺序还是很有用的. 就拿我们上一个例子来说,我们可以这样:首先在我们检查车 ...
- Windows 7如何限制运行特定的应用程序(转载)
AppLocker即"应用程序控制策略",是Windows 7系统中新增加的一项安全功能. 步骤/方法 1 单击"开始"菜单,单击"控制面板" ...
- srpm包的编译方式
基本说明:后缀仅为rpm的包如xxxxx.rpm称作为二进制包 ------ 可以直接安装到架构匹配的系统上; 后缀为src.rpm的包如webkitgtk-2.4.7-1.fc21.src.rpm称 ...
- python之路基础篇
基础篇 1.Python基础之初识python 2.Python数据类型之字符串 3.Python数据类型之列表 4.Python数据类型之元祖 5.Python数据类型之字典 6.Python Se ...
- MySQL笔记--查询语句实践
有一个用户表,属性为 id,age,buytime 创建表以及插入数据的语句 CREATE TABLE USER( id INT, age INT, buytime INT ) ,,); ,,); , ...
- SANS top 20
What Are the Controls?The detailed Consensus Audit Guidelines are posted at http://www.sans.org/cag/ ...
- poj 2892 &&hdu 1540 Tunnel Warfare
http://poj.org/problem?id=2892 #include <cstdio> #include <cstring> #include <algorit ...