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 的大多数功能都已经实现了,为什么不早点开始探索呢?当所有浏览器都开始支持它时,你 ...
随机推荐
- 线上nginx的一次“no live upstreams while connecting to upstream ”分析
线上nginx的一次"no live upstreams while connecting to upstream "分析 线上nginx的一次"no live upst ...
- (Oracle)数据量统计存储过程
本过程适用于Oracle数据量统计. create or replace procedure SP_GET_TAB_COUNT as v_tableName HDSD_TJ.Tablename%typ ...
- 使用“2”个参数调用“SetData”时发生异常:“程序集“
使用"2"个参数调用"SetData"时发生异常:"程序集"Microsoft.VisualStudio.ProjectSystem.VS. ...
- 题解 P3833 【[SHOI2012]魔法树】
题目 直通车 很显然这是个树刨的板子,树上链查询和子树查询 注意: 1.这个点的树根为 0 而不是 1 所以注意读图时点标号 +1 就解决了 2.注意数据范围\(2^{32}\) 然后板子就能过了 n ...
- SpringMVC听课笔记(十二:文件的上传)
1.Spring MVC为文件上传提供了直接的支持,这种支持是通过即插即用的MultipartResolver实现的.Spring用Jakarta Commons FileUpload技术实现了一个M ...
- lodash的debounce函数的使用
最新,在react新项目的开发中使用到了lodash类库的debounce方法,就随手梳理了一下此方法的方便之处 未使用debounce之前 如果不考虑使用debounce,那么在用户连续点击的情况之 ...
- Asp.net core通过自定义特性实现双端数据验证的一些想法
asp.net core集成了非常方便的数据绑定和数据校验机制,配合操作各种easy的vs,效率直接高到飞起. 通过自定义验证特性(Custom Validation Attribute)可以实现对于 ...
- PAT(乙级)2020年秋季考试
比赛链接:https://pintia.cn/market/item/1302816969611366400 7-1 多二了一点 (15分) 题解 模拟. 代码 #include <bits/s ...
- Educational Codeforces Round 91 (Rated for Div. 2) A. Three Indices
题目链接:https://codeforces.com/contest/1380/problem/A 题意 给出一个大小为 $n$ 的排列,找出是否有三个元素满足 $p_i < p_j\ and ...
- Codeforces Round #628 (Div. 2) C. Ehab and Path-etic MEXs(树,思维题)
题意: 给有 n 个点的树的 n-1 条边从 0 到 n-2 编号,使得任意两点路径中未出现的最小数最小的方案. 思路: 先给所有度为 1 的点所在边编号,之后其他点可以随意编排. #include ...