Flatten Arrays & flat() & flatMap()
Flatten Arrays & flat() & flatMap()
https://alligator.io/js/flat-flatmap/
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2019-08-13
*
* @description 编写一个程序将数组扁平化去并除其中重复部分数据,最终得到一个升序且不重复的数组
* @augments
* @example
* @link https://github.com/yygmind/blog/issues/43
*
*/
let arr = [
[1, 2, 2],
[3, 4, 5, 5],
[
6, 7, 8, 9,
[
11, 12,
[
12, 13,
[14]
]
]
],
10,
];
let log = console.log;
const autoFlatArray = (arr = []) => {
let result = [];
const flatArray = (arr = []) => {
for (let i = 0; i < arr.length; i++) {
let item = arr[i];
if (Array.isArray(item)) {
let temp = flatArray(item);
result.concat(temp);
} else {
result.push(item);
}
}
};
flatArray(arr);
// filter & sort asc
result = [...new Set(result)].sort((a, b) => a > b ? 1 : -1);
return result;
};
let result = autoFlatArray(arr);
log(`result all =`, result);
// result all = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ]
/*
const autoFlatArray = (arr = []) => {
let result = [];
const flatArray = (arr = []) => {
for (let i = 0; i < arr.length; i++) {
let item = arr[i];
if (Array.isArray(item)) {
let temp = flatArray(item);
log(`temp`, temp);
result.concat(temp);
log(`result 1 =`, result);
} else {
result.push(item);
log(`result 2 =`, result);
}
}
};
flatArray(arr, []);
// filter & sort asc
result = [...new Set(result)].sort((a, b) => a > b ? 1 : -1);
return result;
};
*/
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2019-08-13
*
* @description
* @augments
* @example
* @link
*
*/
let log = console.log;
var arr = [1, 2, 3, 4];
let result1 = arr.flatMap(x => [x * 2]);
// [2, 4, 6, 8]
// is equivalent to
// let result2 = arr.reduce((acc, x) => acc.concat([x * 2]), []);
// [2, 4, 6, 8]
let result2 = arr1.reduce(
(acc, x) => {
log(`acc =`, acc);// []
log(`x =`, x);// 1
let temp = acc.concat([x * 2]);
log(`temp =`, temp);
return temp;
},
[],// initialValue
);
log(`flat array flatMap`, result1);
log(`flat array reduce`, result2);
/*
arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
The reducer function takes four arguments:
Accumulator (acc)
Current Value (cur)
Current Index (idx)
Source Array (src)
*/
Array & flat
https://github.com/yygmind/blog/issues/43
https://github.com/Advanced-Frontend/Daily-Interview-Question/issues/8#issuecomment-520795766
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
arr.flat(Infinity) === Infinity deep
let arr = [ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10];
let result = Array.from(new Set(arr.flat(Infinity))).sort((a,b)=> a > b ? 1: -1);
console.log(`flat array with unique filter & sort`, result);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
OR
let arr = [ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10];
let result = [...new Set(arr.flat(Infinity))].sort((a,b)=> a > b ? 1: -1);
console.log(`flat array with unique filter & sort`, result);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
Flatten Arrays & flat() & flatMap()的更多相关文章
- Numpy:np.vstack()&np.hstack() flat/flatten
一 . np.vstack: 按垂直方向(行顺序)堆叠数组构成一个新的数组 In[3]: import numpy as np In[4]: a = np.array([[1,2,3]]) a.sh ...
- js array flat all in one
js array flat all in one array flat flatMap flatMap > flat + map https://developer.mozilla.org/en ...
- spark之map与flatMap差别
scala> val m = List(List("a","b"),List("c","d")) m: List[ ...
- monads-are-elephants(转)
介绍monads有点像互联网时代的家庭手工业.我想 “为什么要反对传统?”,但这篇文章将以Scala对待monads的方式来描述. 有个古老的寓言,讲述了几个瞎子第一次摸到大象.一个抱着大象的腿说:“ ...
- 精读《What's new in javascript》
1. 引言 本周精读的内容是:Google I/O 19. 2019 年 Google I/O 介绍了一些激动人心的 JS 新特性,这些特性有些已经被主流浏览器实现,并支持 polyfill,有些还在 ...
- 通过JS将BSAE64生成图片并下载
HTML:<div style="display:block;margin:0 auto;width:638px;height:795px;"><div id=& ...
- 转 c#性能优化秘密
原文:http://www.dotnetperls.com/optimization Generally, using the simplest features of the language pr ...
- Underscore.js 源码学习笔记(上)
版本 Underscore.js 1.9.1 一共 1693 行.注释我就删了,太长了… 整体是一个 (function() {...}()); 这样的东西,我们应该知道这是一个 IIFE(立即执行 ...
- ES10特性详解
摘要: 最新的JS特性. ES10 还只是一个草案.但是除了 Object.fromEntries 之外,Chrome 的大多数功能都已经实现了,为什么不早点开始探索呢?当所有浏览器都开始支持它时,你 ...
随机推荐
- (Oracle)常用的数据库函数
Trim: Trim() 函数的功能是去掉首尾空格. Eg: trim(to_char(level, '00')) Trunc: 1.TRUNC函数为指定元素而截去的日期值. trun ...
- 关于notepad++不能设置中文的解决方法
好久没用notepad++了,最近遇到一个base64的音频文件,想起来notepad++挺好用的 于是安装好 然后发现 最新的notepad++竟然不能显示中文界面 这怎么可以呢! 上网搜 果然 作 ...
- BIO,NIO,AIO 总结
BIO,NIO,AIO 总结 Java 中的 BIO.NIO和 AIO 理解为是 Java 语言对操作系统的各种 IO 模型的封装.程序员在使用这些 API 的时候,不需要关心操作系统层面的知识,也不 ...
- 动态传参,命名空间,嵌套,gloabal,nonlocal
一.动态传参 动态接受位置参数: *参数名 def eat(*food): print(food) #多个参数传递进去,收到的内容是元祖tuple eat("盖浇饭", &quo ...
- VS Code 使用教程详解
一.写在前面 1.为什么选择 \(VS\) \(code\) 一款非常好用的代码编辑器 标准化 \(Language\) \(Service\) \(Protocol\) 内置调试器和标准化 \(De ...
- 微信小程序--使用云开发完成支付闭环
微信小程序--使用云开发完成支付闭环 1.流程介绍 2. 代码实现和逻辑思想描述 云函数统一下单 对应云函数 unipay [CloudPay.unifiedOrder] 函数思路 : 调用云函数封装 ...
- Tomcat 详解URL请求
这里分析一个实际的请求是如何在Tomcat中被处理的,以及最后是怎么样找到要处理的Servlet的?当我们在浏览器中输入http://hostname:port/contextPath/servlet ...
- redis学习教程五《管道、分区》
redis学习教程五<管道.分区> 一:管道 Redis是一个TCP服务器,支持请求/响应协议. 在Redis中,请求通过以下步骤完成: 客户端向服务器发送查询,并从套接字读取,通常以阻 ...
- VSCode-Prettier和ESLint如何和睦共处?
1 在VSCode中单独使用Prettier保存代码自动格式化的配置方法 1.1 为什么要使用Prettier? 手动调整代码格式,不仅低效,而且在团队协作开发中,无法保证代码风格统一,所以需要引入自 ...
- Flink-v1.12官方网站翻译-P018-Event Time
事件时间 在本节中,您将学习如何编写时间感知的Flink程序.请看一下及时流处理,了解及时流处理背后的概念. 关于如何在Flink程序中使用时间的信息请参考windowing和ProcessFunct ...