码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 数组扁平化实现


    数组扁平化方法及实现

    flat的使用:
    Array.prototype.flat() ,数组扁平化遵循如下规则
    不传入参数时,默认拉平一层
    传入一个整数,整数即拉平层数
    传入Infinity,全部拉平
    传入<=0的整数,返回原数组

    const list = [1,2,[3,4,[5,6,[7]]]];
    const flat = list.flat()
    // [1,2,3,4,[5,6,[7]]]
    const flatTwo = list.flat(2)
    // [1,2,3,4,5,6,[7]]
    const flatInfinity = list.flat(Infinity)
    // [1,2,3,4,5,6,7]
    const flatNeg = list.flat(-1)
    // [1,2,[3,4,[5,6,[7]]]]
    console.log(flat,flatTwo,flatInfinity,flatNeg)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    lodash flatten、flattenDeep、flattenDepth函数的使用
    flatten:减少一级array嵌套深度
    flattenDeep:将array递归为一维数组
    flattenDepth:根据 depth 递归减少 array 的嵌套层级

    const list = [1,2,[3,4,[5,6,[7]]]];
    _.flatten(list);
     //[1,2,3,4,[5,6,[7]]]
     _.flattenDeep(list);
     //[1,2,3,4,5,6,7]
     _.flattenDepth(list, 2);
     //[1,2,3,4,5,6,[7]]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    实现要点:
    1、遍历数组
    2、判断元素是否为数组
    3、将数组元素展开一层

    1、遍历数组,遍历数组的方法有很多,只要是能遍历数组取到数组每一个元素的方法都是一个解决方案
    for循环、for…of for…in forEach()

    const list = [1,2,[3,4,[5,6,[7]]]];
    for (let index = 0; index < list.length; index++) {
      console.log(list[index]);
    }
    for (const value of list) {
      console.log(value);
    }
    for (const index in list) {
      console.log(list[index]);
    }
    list.forEach(element => {
      console.log(element);
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2、判断元素是否为数组
    instanceof、isArray、construator、Object.prototype.String.call()

    const list = [1, 2, [3, 4, [5, 6, [7]]]];
    list instanceof Array;
    // true
    list.constructor === Array;
    // true
    Object.prototype.toString.call(list) === "[object Array]";
    // true
    Array.isArray(list);
    // true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3、将数组展开一层
    扩展运算符+concat方法、concat+apply方法、toString+split方法

    const list = [1, 2, [3, 4, [5, 6, [7]]]];
    [].concat(...list);
    [].concat.apply([], list);
    list.toString().split(',').map(v=>parseInt(v))
    
    • 1
    • 2
    • 3
    • 4

    按照1、2、3步骤实现:

    const arr = [1, 2, 3, 4, [1, 2, 3, [1, 2, 3, [1, 2, 3]]], 5, "string", { name: "名称12" }];
    // concat + 递归
    function flat(arr) {
      let arrResult = [];
      arr.forEach(item => {
        if (Array.isArray(item)) {
          arrResult = arrResult.concat(arguments.callee(item));   // 递归
          // 或者用扩展运算符
          // arrResult.push(...arguments.callee(item));
        } else {
          arrResult.push(item);
        }
      });
      return arrResult;
    }
    flat(arr)
    // [1, 2, 3, 4, 1, 2, 3, 1, 2, 3, 1, 2, 3, 5, "string", { name: "名称12" }];
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
  • 相关阅读:
    服务器git安装python包失败,如何手动下载github项目包并安装到虚拟环境中(简单易懂)
    LeetCode每日一题(786. K-th Smallest Prime Fraction)
    Docker 网络学习
    jeecg vue3版本集成达梦数据库
    武警三维数字沙盘电子沙盘虚拟现实模拟推演大数据人工智能开发教程第15课
    测试外包服务 | 从人员外包到测试工具、测试平台,提供全方位的测试解决方案~
    【c语言】100行代码搞定电子琴
    谷粒商城篇章6 ---- P193-P210 ---- 异步&线程池&商品详情【分布式高级篇三】
    初识RabbitMQ - 安装 - 搭建基础环境
    《ON JAVA》学习笔记3:Java和C++的一些不同点
  • 原文地址:https://blog.csdn.net/weixin_42254016/article/details/127756930
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号