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

    1:概念 1.1.1 静态网站和动态网站 静态网站:不支持数据交互的网站,(html,htm) 动态网站:支持数据交互的网站 实现动态网站的技术: 实现技术 网站后缀 ASP技术 .asp PHP . ...

  2. css选择器有哪些,选择器的权重的优先级

    选择器类型 1.ID #id 2.class .class 3.标签 p 4.通用 * 5.属性 [type="text"] 6.伪类 :hover 7.伪元素 ::first-l ...

  3. 洛谷P3850 书架

    题目描述 Knuth先生家里有个精致的书架,书架上有N本书,如今他想学到更多的知识,于是又买来了M本不同的新书.现在他要把新买的书依次插入到书架中,他已经把每本书要插入的位置标记好了,并且相应的将它们 ...

  4. day133:2RenMJ:TypeScript的变量&函数&类&接口

    目录 1.变量 2.函数 3.类 4.接口 1.变量 1.变量的声明 // 1.即指定数据类型 也指定值 var 变量名:类型 = 值; eg:var username:string = " ...

  5. CSS3 Flex Box 弹性盒子、弹性布局

    目录 1. 概要 2. justify-content 属性 3. align-items 属性 4. flex-wrap 属性 5. align-content 属性 6. 居中 7. align- ...

  6. Linux——软件安装

    Linux--软件安装 一.gcc 二.make 三.rpm 四.yum 一.gcc gcc是Linux上面最标准的C语言的编译程序,用来源代码的编译链接. gcc -c hello.c 编译产生目标 ...

  7. zabbix监控设备结果异常问题

    1.现象描述 paloalto防火墙升级后发现zabbix监控其CPU.风扇等硬件信息和端口流量数据错误. 2.现象分析 由于zabbix并没有做过任何调整,防火墙也只是升级.根据现象并不好排查.但是 ...

  8. 搭建 mariadb 数据库主从同步

    一.主(master)数据库配置 1. my.cnf 添加配置 [mariadb] log-bin server_id=1 log-basename=master1 binlog-format=mix ...

  9. docker(2)CentOS 7安装docker环境

    前言 前面一篇学了mac安装docker,这篇来学习在linux上安装docker 环境准备 Docker支持以下的CentOS版本,目前,CentOS 仅发行版本中的内核支持 Docker. Doc ...

  10. Redis-第七章节-持久化

    目录 概述 RDB AOF 如何选择持久化机制 1.概述 Redis 是内存数据库,如果不能将内存中的数据保存到磁盘中,那么一旦服务器进程退出,服务器的数据库数据也会消失,所以Redis提供了持久化的 ...