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() - ...
随机推荐
- logging philosophy 日志哲学
Go kit - Frequently asked questions https://gokit.io/faq/ Logging - Why is package log so different? ...
- pywin32 pywin32 docx文档转html页面 word doc docx 提取文字 图片 html 结构
https://blog.csdn.net/X21214054/article/details/78873338# python docx文档转html页面 - 程序猿tx - 博客园 https:/ ...
- JVM 详解,大白话带你认识 JVM
前言 如果在文中用词或者理解方面出现问题,欢迎指出.此文旨在提及而不深究,但会尽量效率地把知识点都抛出来 一.JVM的基本介绍 JVM 是 Java Virtual Machine 的缩写,它是一个虚 ...
- 【Redis 分布式锁】(1)一把简单的“锁”
原文链接:https://www.changxuan.top/?p=1230 在单体架构向分布式集群架构演进的过程中,项目中必不可少的一个功能组件就是分布式锁.在开发团队有技术积累的情况下,做为团队的 ...
- ReentrantReadWriteLock读写锁简单原理案例证明
ReentrantReadWriteLock存在原因? 我们知道List的实现类ArrayList,LinkedList都是非线程安全的,Vector类通过用synchronized修饰方法保证了Li ...
- vue3系列:vue3.0自定义虚拟滚动条V3Scroll|vue3模拟滚动条组件
基于Vue3.0构建PC桌面端自定义美化滚动条组件V3Scroll. 前段时间有分享一个Vue3 PC网页端弹窗组件,今天带来最新开发的Vue3.0版虚拟滚动条组件. V3Scroll 使用vue3. ...
- 39.NFS(网络文件系统)
要共享文件的主机都是Linux系统时推荐在客户端部署NFS 服务来共享文件. NFS(网络文件系统)服务可以将远程Linux 系统上的文件共享资源挂载到本地主机的目录上,从而使得本地主机(Linux ...
- 34.vsftpd服务程序--虚拟用户模式
1.创建用于进行FTP 认证的用户数据库文件,其中奇数行为账户名,偶数行为密码. [root@localhost ~]# cd /etc/vsftpd/ [root@localhost vsftpd] ...
- Kibana,Logstash 和 Cerebro 的安装运行
公号:码农充电站pro 主页:https://codeshellme.github.io 1,安装 Kibana Kibana 用于数据可视化,我们可以进入到 Kibana 下载页面下载 Kibana ...
- 2019牛客暑期多校训练营(第八场)B-Beauty Values(期望线性性)
>传送门< 题意:思路:期望的线性性(可加性),比赛的时候写的代码超级无敌长,不过值得欣慰的是一发AC了,官方的题解写的还不错~ 我们可以把每种数字对答案的贡献分开来计算,即枚举每个数字, ...