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. connection-backoff ConnectionBackoff Strategy 回退

    grpc/connection-backoff.md at master · grpc/grpc https://github.com/grpc/grpc/blob/master/doc/connec ...

  2. Boyer-Moore 投票算法

    Boyer-Moore 投票算法 http://theory.stanford.edu/~trevisan/cs154-12/notestream.pdf 众数

  3. 【LinuxShell】echo用法详解

    使用echo打印信息到终端 1 [Shell]echo "yz test" 2 yz test 3 [Shell]echo yz test 4 yz test 5 [Shell]e ...

  4. 机器学习基础——规则化(Regularization)

    在机器学习中,我们一直期望学习一个泛化能力(generalization)强的函数只有泛化能力强的模型才能很好地适用于整个样本空间,才能在新的样本点上表现良好. \[y=a+bx+cx^2+dx^3\ ...

  5. qbxt 学习笔记 10.2 晚

    目录 整除性 素数 组合数 Lucas 定理: 整除性 直接搬 ppt 特殊的整除性质 素数 素数定理: 线性筛: 原理:一个合数只由其最大素因子筛去. 代码: 组合数 Lucas 定理: \[\bi ...

  6. springboot+Jenkins+docker-compose自动部署项目实践

    DevOps思想 一个开发.测试.运维的整个过程的思想. plan:需求.计划 code:编码 build:构建 test: 测试 release:发布版本 deploy:部署 operate:项目运 ...

  7. 服务注册发现与注册中心对比-Eureka,Consul,Zookeeper,Nacos对比

    服务注册发现与注册中心对比-Eureka,Consul,Zookeeper,Nacos对比 注册中心简介 流程和原理 基础流程 核心功能 1.Eureka.Consul.Zookeeper三者异同点 ...

  8. Java8种排序算法学习

    冒泡排序 public class test { public static void main(String[] args) { // TODO Auto-generated method stub ...

  9. Spring MVC参数处理

    使用Servlet API作为参数 HttpServletRequest HttpServletResponse HttpSession 使用流作为参数 总结 Spring MVC通过分析处理处理方法 ...

  10. ajax 用fom提交

    $.ajax({ type : "POST", url : "${ctx}/credit/LoanauditCtrl/qwe.do?hetong="+heton ...