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

  1. js Array All In One

    js Array All In One array 方法,改变原数组(长度),不改变原数组(长度) https://developer.mozilla.org/en-US/docs/Web/JavaS ...

  2. js Array数组的使用

    js Array数组的使用   Array是javascript中的一个事先定义好的对象(也可以称作一个类),可以直接使用 创建Array对象 var array=new Array(): 创建指定元 ...

  3. 从Chrome源码看JS Array的实现

    .aligncenter { clear: both; display: block; margin-left: auto; margin-right: auto } .crayon-line spa ...

  4. js Array 方法总结

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. js & array to string

    js & array to string https://stackoverflow.com/questions/13272406/convert-string-with-commas-to- ...

  6. From Ruby array to JS array in Rails- 'quote'?

    From Ruby array to JS array in Rails- 'quote'? <%= raw @location_list.as_json %>

  7. 解决js array的key不为数字时获取长度的问题

    最近写js时碰到了当数组key不为数字时,获取数组的长度为0 的情况. 1.问题场景 var arr = new Array(); arr[‘s1‘] = 1001; console.log(arr. ...

  8. JS Array.reverse 将数组元素颠倒顺序

    <pre><script type="text/javascript"> //JS Array.reverse 将数组元素颠倒顺序//在JavaScript ...

  9. js Array 中的 map, filter 和 reduce

    原文中部分源码来源于:JS Array.reduce 实现 Array.map 和 Array.filter Array 中的高阶函数 ---- map, filter, reduce map() - ...

随机推荐

  1. 编译Nacos,解决No Server available 以及 failed to req API__nacos_v1_ns_instance after all servers

    问题描述:如图,显示没有服务可用 仔细看控制台,看到上面Error部分,相关参数没有读取到配置信息,那么配置信息这块似乎是有问题,赶紧看看IDE对配置信息的扫描情况: 可以看到有信息了,但是报错:No ...

  2. python_3 装饰器之初次见面

    装饰器 定义:本质是函数,(只不过是用来装饰其他函数而已),就是为其他函数添加附加功能 原则: 1. 不能修改被修饰函数的源代码 2.不能修改被修饰函数的调用方式 实现装饰器的知识储备 1.函数即&q ...

  3. 博客-livevent-stl-cpp-nginx

    https://blog.csdn.net/move_now/article/category/6420121

  4. 在IDEA中用三个jar包链接MongoDB数据库——实现增删改查

    安装Robo 3T连接MongoDB数据库教程:https://blog.csdn.net/baidu_39298625/article/details/98845789 使用Robo 3T操作Mon ...

  5. java.lang.IllegalStateException Unable to find a @SpringBootConfiguration错误解决方案

    问题描述:java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @Co ...

  6. 算法总结篇---KMP算法

    目录 写在前面 例题 剪花布条 Radio Transmission OKR-Periods of Words 似乎在梦中见过的样子 Censoring 写在前面 仅为自用,不做推广 一起来看猫片吧! ...

  7. XCTF-黑客精神

    杂言 前段时间键盘坏了,电脑硬盘也坏了,买东西装系统再偷个懒放了一周左右假.期间学习巩固了一下安卓开发的知识.用了固态才知道什么叫纵享丝滑,当初就不该省这个钱. 前期工作 查壳,无.运行,点击按钮就跳 ...

  8. 用RabbitMQ了好几年之后,我总结出来5点RabbitMQ的使用心得

    大概从 2013 年开始,我就开始了自己和 RabbitMQ 的接触,到现在已经有七年多了. 在这七年中,既有一些对 RabbitMQ 的深度体验,更有无数的血泪史. 而根据我这么多年的使用经验,我将 ...

  9. HTML5 网页制作技巧

    本文总结自由人民邮电出版社出版的<HTML.CSS.Javascript网页制作>. 总结进行学习,并分享给同样编写HTML5的朋友. 1:背景音乐的添加 <embed src=&q ...

  10. java生成Https证书,及证书导入的步骤和过程

    以下是相关的Tomcat,JDK和Windows环境: Tomcat版本:tomcat-7.0.55 JDK版本: jdk1.6.0 目录所在的位置: Serve的目录:D:\server\tomca ...