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 的大多数功能都已经实现了,为什么不早点开始探索呢?当所有浏览器都开始支持它时,你 ...
随机推荐
- celery 动态配置定时任务
How to dynamically add or remove tasks to celerybeat? · Issue #3493 · celery/celery https://github.c ...
- vue初始化页面闪动问题
使用vue开发时,在vue初始化之前,由于div是不归vue管的,所以我们写的代码在还没有解析的情况下会容易出现花屏现象,看到类似于{{message}}的字样,虽然一般情况下这个时间很短暂,但是我们 ...
- Vue中双向数据绑定是如何实现的?
vue 双向数据绑定是通过 数据劫持 结合 发布订阅模式的方式来实现的, 也就是说数据和视图同步,数据发生变化,视图跟着变化,视图变化,数据也随之发生改变:核心:关于VUE双向数据绑定,其核心是 Ob ...
- RocketMQ 的几个简单问题与答案
1 单机版消息中心 一个消息中心,最基本的需要支持多生产者.多消费者,例如下: class Scratch { public static void main(String[] args) { // ...
- loj10014数列分段二
10014. 「一本通 1.2 练习 1」数列分段 II 题目描述 对于给定的一个长度为 n 的正整数数列 A ,现要将其分成 m 段,并要求每段连续,且每段和的最大值最小. 例如,将数列 4,2,4 ...
- 【PY从0到1】第七节 函数
# 7 第七节 函数 # 函数对于编程语言来说是一块重量级的内容. # 他可以实现或者简化编写的代码. # 编写好特定功能的函数后,就可以重复调用函数来完成任务. # 下面我们就用函数的形式来封装前面 ...
- 十三:SpringBoot-基于Yml配置方式,实现文件上传逻辑
SpringBoot-基于Yml配置方式,实现文件上传逻辑 1.文件上传 2.搭建文件上传界面 2.1 引入页面模板Jar包 2.2 编写简单的上传页面 2.3 配置页面入口 3.SpringBoot ...
- Java8种排序算法学习
冒泡排序 public class test { public static void main(String[] args) { // TODO Auto-generated method stub ...
- GIS基本概念,空间分析
GIS基本概念,空间分析 一.GIS基本概念 1.1 要素模型(Feature) 1.2 矢量数据 1.3 空间分析 1.3.1 空间查询和空间量算 1.3.2 缓冲区分析 1.3.3 叠加分析 1. ...
- 如何使用命令将文件夹中的文件名(包括路径)写入到txt文件中
在cmd中使用 cd /d 路径,进入当前文件夹中 使用 dir /s /b > 0.txt 如图: