js array flat all in one
js array flat all in one
array flat
flatMap
flatMap > flat + map
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap
var new_array = arr.flatMap(function callback(currentValue[, index[, array]]) {
// return element for new_array
}[, thisArg])
flat
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
var newArray = arr.flat([depth]);
// depth defaults 1
// The depth level specifying how deep a nested array structure should be flattened.
map
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
reduce
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
Array.flatAll
// prototype
demos
['My dog', 'is awesome'].map(words => words.split(' '))
//[ [ 'My', 'dog' ], [ 'is', 'awesome' ] ]
['My dog', 'is awesome'].flatMap(words => words.split(' '))
//[ 'My', 'dog', 'is', 'awesome' ]
const log = console.log;
const nested = [['', ''], ['']];
const flattened = nested.flat(nested.length);
log(flattened);
// ['', '', '']
const arr1 = [1, 2, 3, [1, 2, 3, 4, [2, 3, 4]]];
function flattenDeep(arr) {
return arr.reduce(
(acc, val) =>
Array.isArray(val) ? acc.concat(flattenDeep(val)) : acc.concat(val),
[],
);
}
flattenDeep(arr1);
// [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]
lodash
lodash.flatten
lodash.flattendeep
lodash.flattendepth
https://www.npmjs.com/package/lodash.flatten
https://www.npmjs.com/package/lodash.flattendeep
https://www.npmjs.com/package/lodash.flattendepth
https://lodash.com/docs/4.17.15#flatten
_.flatten([1, [2, [3, [4]], 5]]);
// => [1, 2, [3, [4]], 5]
_.flattenDeep([1, [2, [3, [4]], 5]]);
// => [1, 2, 3, 4, 5]
const arr = [1, [2, [3, [4]], 5]];
_.flattenDepth(arr, 1);
// => [1, 2, [3, [4]], 5]
_.flattenDepth(arr, 2);
// => [1, 2, 3, [4], 5]
nested array to object
Object.fromEntries()
const log = console.log;
const arr = [
["a", 1],
["b", true],
["c", []],
["d", {}],
["e", "nested array to object"],
];
const obj = Object.fromEntries(arr);;
log(obj)
refs
https://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays
https://flaviocopes.com/javascript-flatten-array/
https://linguinecode.com/post/new-es2019-javascript-features
https://github.com/lgwebdream/FE-Interview/issues/8
https://atendesigngroup.com/articles/array-map-filter-and-reduce-js
https://www.samanthaming.com/tidbits/71-how-to-flatten-array-using-array-flat/
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
js array flat all in one的更多相关文章
- js Array All In One
js Array All In One array 方法,改变原数组(长度),不改变原数组(长度) https://developer.mozilla.org/en-US/docs/Web/JavaS ...
- js Array数组的使用
js Array数组的使用 Array是javascript中的一个事先定义好的对象(也可以称作一个类),可以直接使用 创建Array对象 var array=new Array(): 创建指定元 ...
- 从Chrome源码看JS Array的实现
.aligncenter { clear: both; display: block; margin-left: auto; margin-right: auto } .crayon-line spa ...
- js Array 方法总结
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- js & array to string
js & array to string https://stackoverflow.com/questions/13272406/convert-string-with-commas-to- ...
- From Ruby array to JS array in Rails- 'quote'?
From Ruby array to JS array in Rails- 'quote'? <%= raw @location_list.as_json %>
- 解决js array的key不为数字时获取长度的问题
最近写js时碰到了当数组key不为数字时,获取数组的长度为0 的情况. 1.问题场景 var arr = new Array(); arr[‘s1‘] = 1001; console.log(arr. ...
- JS Array.reverse 将数组元素颠倒顺序
<pre><script type="text/javascript"> //JS Array.reverse 将数组元素颠倒顺序//在JavaScript ...
- js Array 中的 map, filter 和 reduce
原文中部分源码来源于:JS Array.reduce 实现 Array.map 和 Array.filter Array 中的高阶函数 ---- map, filter, reduce map() - ...
随机推荐
- IDEA 简介
什么是IDEA IDEA 全称 IntelliJ IDEA,是 Java 语言开发的集成环境,IntelliJ 在业界被公认为最好的 Java 开发工具之一,尤其在智能代码助手.代码自动提示.重构.J ...
- 二进制GCD
目录 写在前面 具体实现: Code 写在前面 全程抄书 想要进一步提高求 \(\gcd\) 的效率,可以通过不断去除因子 \(2\) 来降低常数,这就是"二进制 \(\gcd\) &quo ...
- 七:SpringBoot-集成Redis数据库,实现缓存管理
SpringBoot-集成Redis数据库,实现缓存管理 1.SpringBoot集成Redis 1.1 核心依赖 1.2 配置文件 1.3 简单测试案例 1.4 自定义序列化配置 1.5 序列化测试 ...
- Display属性学习总结
HTMl元素根据表现形式,常见的可以分为两类. (1)块元素(block) (2)行内元素(inline). 当然,除了以上两种元素类型外,还有inline-block.table-cell等元素类型 ...
- Grafana部署监控docker服务
Grafana部署监控docker服务 一.使用InfluxDB+cAdvisor+Grafana配置Docker监控 1.1Docker监控组件 1.2cAdvisor: 1.3Docker监控安装 ...
- JDBC的操作步骤和实例()
加载JDBC驱动程序 提供JDBC连接的URL 创建数据库的连接 创建一个Statement 执行SQL语句 处理结果 关闭JDBC对象 实例JdbcUtils 创建一个JDBC程序包含7个步骤: 1 ...
- 读取EXCEL文档解析工具类
package test;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException ...
- XV6学习(12)Lab lock: Parallelism/locking
代码在github上 这一次实验是要对XV6内部的锁进行优化,减少锁争用,提高系统的性能. Memory allocator (moderate) 第一个实验是对XV6内核的内存页面分配器进行改进,改 ...
- Educational Codeforces Round 97 (Rated for Div. 2) E. Make It Increasing(最长非下降子序列)
题目链接:https://codeforces.com/contest/1437/problem/E 题意 给出一个大小为 \(n\) 的数组 \(a\) 和一个下标数组 \(b\),每次操作可以选择 ...
- codeforces Looksery Cup 2015 C. The Game Of Parity
There are n cities in Westeros. The i-th city is inhabited by ai people. Daenerys and Stannis play t ...