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()的更多相关文章

  1. 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 ...

  2. js array flat all in one

    js array flat all in one array flat flatMap flatMap > flat + map https://developer.mozilla.org/en ...

  3. spark之map与flatMap差别

    scala> val m = List(List("a","b"),List("c","d")) m: List[ ...

  4. monads-are-elephants(转)

    介绍monads有点像互联网时代的家庭手工业.我想 “为什么要反对传统?”,但这篇文章将以Scala对待monads的方式来描述. 有个古老的寓言,讲述了几个瞎子第一次摸到大象.一个抱着大象的腿说:“ ...

  5. 精读《What's new in javascript》

    1. 引言 本周精读的内容是:Google I/O 19. 2019 年 Google I/O 介绍了一些激动人心的 JS 新特性,这些特性有些已经被主流浏览器实现,并支持 polyfill,有些还在 ...

  6. 通过JS将BSAE64生成图片并下载

    HTML:<div style="display:block;margin:0 auto;width:638px;height:795px;"><div id=& ...

  7. 转 c#性能优化秘密

    原文:http://www.dotnetperls.com/optimization Generally, using the simplest features of the language pr ...

  8. Underscore.js 源码学习笔记(上)

    版本 Underscore.js 1.9.1 一共 1693 行.注释我就删了,太长了… 整体是一个 (function() {...}());  这样的东西,我们应该知道这是一个 IIFE(立即执行 ...

  9. ES10特性详解

    摘要: 最新的JS特性. ES10 还只是一个草案.但是除了 Object.fromEntries 之外,Chrome 的大多数功能都已经实现了,为什么不早点开始探索呢?当所有浏览器都开始支持它时,你 ...

随机推荐

  1. super 多重继承 super() function with multilevel inheritance

    Python | super() function with multilevel inheritance - GeeksforGeeks https://www.geeksforgeeks.org/ ...

  2. Urlrewrite

    Urlrewrite  地址重写,用户得到的全部都是经过处理后的URL地址 过滤用户的所有请求,符合规则的便对其进行重定向 rule结点中from默认使用的正则表达式来匹配,若用户访问服务器时的URL ...

  3. 九:SpringBoot-整合Mybatis框架,集成分页助手插件

    九:SpringBoot-整合Mybatis框架,集成分页助手插件 1.Mybatis框架 1.1 mybatis特点 1.2 适用场景 2.SpringBoot整合MyBatis 2.1 核心依赖 ...

  4. 22.firewalld

    1.firewalld 中常用的区域名称及策略规则 2.firewalld-cmd 命令中使用的参数以及作用 与 Linux 系统中其他的防火墙策略配置工具一样,使用firewalld 配置的防火墙策 ...

  5. php文件下载的实现(header)

    php文件下载的实现(header) $file_xls=$path;    //   文件的保存路径 $example_name=basename($file_xls);  //获取文件名   he ...

  6. jvm-本地方法接口

    什么是本地方法 简单地讲,一个Native Methodt是一个Java调用非Java代码的接囗.一个Native Method是这样一个Java方法:该方法的实现由非Java语言实现,比如C.这个特 ...

  7. Flink-v1.12官方网站翻译-P013-Timely Stream Processing

    及时的流处理 介绍 及时流处理是有状态流处理的一种扩展,其中时间在计算中起着一定的作用.其中,当你做时间序列分析时,当做基于某些时间段(通常称为窗口)的聚合时,或者当你做事件处理时,事件发生的时间很重 ...

  8. 爬虫入门到放弃系列02:html网页如何解析

    前言 上一篇文章讲了爬虫的概念,本篇文章主要来讲述一下如何来解析爬虫请求的网页内容. 一个简单的爬虫程序主要分为两个部分,请求部分和解析部分.请求部分基本一行代码就可以搞定,所以主要来讲述一下解析部分 ...

  9. 查看文件MD5值

    Windows 打开命令窗口(Win+R),然后输入cmd ·输入命令certutil -hashfile 文件绝对路径 MD5 Linux MD5算法常常被用来验证网络文件传输的完整性,防止文件被人 ...

  10. GPLT L2-010 排座位 (并查集)

    Tips: 数据范围较小时可把二维数组当做map<pair<int,int>,int>使用. #include <bits/stdc++.h> using name ...