亮神知识库 亮神知识库
首页
  • 手写代码

    • 手写代码系列
  • 基础知识

    • 基础
    • JS底层
    • CSS
  • 原理
  • 浏览器
  • HTTP
  • 网络安全
  • babel
  • webpack基础
  • webpack进阶
  • Vite
  • TypeScript
  • Vue2
  • Vue3
  • Node基础

    • glob
    • 模块化机制
    • 事件循环
    • KOA2框架原理
    • Node子进程
    • cluster原理(了解)
  • 教育行业2021

    • B端
    • C端
    • 工具
  • 游戏行业2025
  • 刷题
  • 杂(待整理)
  • 学习
  • 面试
  • 实用技巧
  • 心情杂货
  • 年度总结
  • 友情链接
关于
  • 分类
  • 标签
  • 归档
  • 收藏
GitHub (opens new window)

亮神

前程明亮,未来可期
首页
  • 手写代码

    • 手写代码系列
  • 基础知识

    • 基础
    • JS底层
    • CSS
  • 原理
  • 浏览器
  • HTTP
  • 网络安全
  • babel
  • webpack基础
  • webpack进阶
  • Vite
  • TypeScript
  • Vue2
  • Vue3
  • Node基础

    • glob
    • 模块化机制
    • 事件循环
    • KOA2框架原理
    • Node子进程
    • cluster原理(了解)
  • 教育行业2021

    • B端
    • C端
    • 工具
  • 游戏行业2025
  • 刷题
  • 杂(待整理)
  • 学习
  • 面试
  • 实用技巧
  • 心情杂货
  • 年度总结
  • 友情链接
关于
  • 分类
  • 标签
  • 归档
  • 收藏
GitHub (opens new window)
  • 基础

  • 手写代码

    • 手写类型转换
    • 手写累加、累乘函数
    • 手写new
    • 手写深拷贝
    • 手写Object.create
    • 手写继承
    • 手写extends
    • 手写instanceof
    • 手写call、apply、bind
    • 手写jsonp
    • 手写getQueryString
    • 手写setInterval
    • 手写防抖与节流
    • 手写对象属性值迭代器
    • 手写分时函数
    • 手写事件委托
    • 手写图片懒加载
    • 手写原生Ajax请求
    • 手写AOP装饰函数
    • 手写柯里函数
    • 手写数组扁平化flat
      • Array.prototype.reduce
      • 手写Array.prototype.reduce
      • 手写Array.prototype.flat
    • 手写数组去重
    • 手写eventEmit类
    • 手写Vue数据响应式
    • 手写Vue nextTick
    • 手写Promise
  • JS底层深入

  • CSS

  • 基础
  • 手写代码
0zcl
2021-06-18
目录

手写数组扁平化flat

# Array.prototype.reduce

<code>arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])</code>

  • accumulator: 累计器。上一次执行回调返回的累计值。如果是第一次执行的话将会采用initialValue,initialValue不存在的话将会采用数组的第一个元素
  • currentValue:当前值
  • index:索引
  • array: 数组 计算数组各个元素相加的和
const numbers = [1, 2, 3, 4]
const sum = numbers.reduce((accumulator, currentVal) => accumulator + currentVal)
// 初始值为 10
const sum2 = numbers.reduce((accumulator, currentVal) => accumulator + currentVal, 10)
console.log(sum) // 10
console.log(sum2) // 20
1
2
3
4
5
6

# 手写Array.prototype.reduce

注意

实现Array.prototype.reduce

答:

Array.prototype.myReduce = function(callback, initVal) {
  let accumulator = initVal ? initVal : this[0]
  for (let index=initVal ? 0 : 1, len=this.length; index < len; index++) {
    accumulator = callback(accumulator, this[index])
  }
  return accumulator
}
// 测试
const sum2 = numbers.myReduce((accumulator, currentVal) => accumulator + currentVal, 10)
console.log(sum) // 10
console.log(sum2) // 20
1
2
3
4
5
6
7
8
9
10
11

思路:

  • reduce接口返回<code>accumulator</code>累加值
  • 如果<code>initVal</code>存在,累加值初始为initVal;如果不存在,累加值初始为数组的第一个元素
  • 循环数组,调用reduce传入的函数callback, callback返回值为新的累加值

# 手写Array.prototype.flat

注意

实现数组扁平化:Array.prototype.flat

答:Array.prototype.flat([depth]), depth默认为1

Array.prototype.flatten = function(depth = 1) {
  let res = []
  // 终止条件
  if (depth <= 0) {
    res = this.slice() // 浅拷贝
  } else {
    res = this.reduce((accumulator, currentVal) => {
      return accumulator.concat(currentVal instanceof Array ? currentVal.flatten.call(currentVal, depth - 1) : currentVal)
    }, [])
  }
  return res
}
// 测试
var arr = ['zcl', [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10]
1
2
3
4
5
6
7
8
9
10
11
12
13
14

reduce

思路:需要使用递归

  • 如果depth<=0, 则浅拷贝后返回
  • 若depth>0,使用reduce进行遍历,initVal为[],即把遍平后的数据放入initVal。
    • 当前值为数组,表明还未拉平,需使用递归
    • 当前值不为数组,表明已拉平,把当前值加入累加值

手写flat,基本理解,感觉让我再写一次,可能写不出来了!

提示

ES6的 <code>Array.prototype.flat()</code>有兼容性问题, 不建议使用。可以使用自己实现的flat

参考: Array.prototype.reduce (opens new window)

编辑 (opens new window)
上次更新: 2025/07/20, 06:21:22
手写柯里函数
手写数组去重

← 手写柯里函数 手写数组去重→

最近更新
01
2024年
07-20
02
2023年
07-20
03
2022年
07-20
更多文章>
Theme by Vdoing | Copyright © 2025-2025 亮神 | MIT License | 桂ICP备2024034950号 | 桂公网安备45142202000030
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式