js-手写数组常用方法的实现

前言

数组的api比较多,下面主要列举一些常用的api实现,主要是参考MDN上Array的Polyfill来实现的。
MDN链接:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array

forEach

forEach主要用于遍历数组

/**
 * @description: forEach简单实现
 * @param {Function} callback 回调函数 函数中接受(当前遍历的元素,当前遍历索引,当前遍历的数组)三个参数
 * @param {Any} thisArg 传入可以改变callback的this指向
 */
Array.prototype._forEach = function (callback, thisArg) {
  if (typeof callback !== "function") {
    throw new TypeError(callback + ' is not a function')
  }
  let index = -1 // 索引
  const { length: len } = this // 数组长度
  while (++index !== len) {
    if (index in this) { // 数组下标非连续的情况
      callback.call(thisArg, this[index], index, this)
    }
  }
}

filter

filter主要用于过滤数组

/**
 * @description: filter简单实现
 * @param {Function} callback 回调函数 函数中接受(当前遍历的元素,当前遍历索引,当前遍历的数组)三个参数
 * @param {Any} thisArg 传入可以改变callback的this指向
 */
 Array.prototype._filter = function (callback, thisArg) {
  if (typeof callback !== "function") {
    throw new TypeError(callback + ' is not a function')
  }
  let index = -1, newIdx = 0 // 索引、返回新数组索引
  const { length: len } = this // 原数组长度
  const result = [] // 返回的新数组
  while (++index !== len) {
    if (index in this) { // 数组下标非连续的情况
      if (callback.call(thisArg, this[index], index, this)) {
        result[newIdx++] = this[index]
      }
    }
  }
  return result
}

map

map中callback必须要有返回值,结果返回一个由callback返回值组成的新数组。

/**
 * @description: map简单实现
 * @param {Function} callback 回调函数 函数中接受(当前遍历的元素,当前遍历索引,当前遍历的数组)三个参数
 * @param {Any} thisArg 传入可以改变callback的this指向
 * @return {Array} 返回callback返回值组成的新数组
 */
 Array.prototype._map = function (callback, thisArg) {
  if (typeof callback !== "function") {
    throw new TypeError(callback + ' is not a function')
  }
  let index = -1 // 索引
  const { length: len } = this // 数组长度
  const result = [] // 返回的新数组
  while (++index !== len) {
    if (index in this) { // 数组下标非连续的情况
      result[index] = callback.call(thisArg, this[index], index, this)
    }
  }
  return result
}

reduce

reduce相对比较复杂,需要判断是否传入初始值,并且每次循环的结果要传递到下一次循环当中。

/**
 * @description: reduce简单实现
 * @param {Function} callback 回调函数 函数中接受(当前遍历的元素,当前遍历索引,当前遍历的数组)三个参数
 * @param {Any} thisArg 传入可以改变callback的this指向
 */
Array.prototype._reduce = function (...args) {
  const callback = args[0] // 回调函数
  const { length: len } = this // 数组长度
  const { length: argLen } = args // 参数长度
  let index = -1 // 数组下标
  let result

  if (typeof callback !== "function") {
    throw new TypeError(callback + ' is not a function')
  }
  
  // 存在第二个参数,作为函数的初始值initialValue
  if (argLen >= 2) {
    result = args[1]
  } else {
    // 找到数组第一项的下标
    while (index < len && !(index in this)) {
      index++
    }
    // 数组为空并且没有初始值的情况
    if (index >= len) {
      throw new TypeError('Reduce of empty array with no initial value')
    }
    result = arr[index++]
  }
  while (++index !== len) {
    if (index in this) {
      // 将每次返回的结果传入下一次循环
      result = callback(result, this[index], index, this)
    }
  }
  return result
}

find

find找到符合条件的项就返回,没有找到返回undefined。

/**
 * @description: find简单实现
 * @param {Function} callback 回调函数 函数中接受(当前遍历的元素,当前遍历索引,当前遍历的数组)三个参数
 * @param {Any} thisArg 传入可以改变callback的this指向
 */
 Array.prototype._find = function (callback, thisArg) {
  if (typeof callback !== "function") {
    throw new TypeError(callback + ' is not a function')
  }
  let index = -1 // 索引
  const { length: len } = this // 原数组长度
  while (++index !== len) {
    if (callback.call(thisArg, this[index], index, this)) {
      return this[index] // 如果是findIndex则return index
    }
  }
}

some

some和find除了返回值实现步骤一样

/**
 * @description: some简单实现
 * @param {Function} callback 回调函数 函数中接受(当前遍历的元素,当前遍历索引,当前遍历的数组)三个参数
 * @param {Any} thisArg 传入可以改变callback的this指向
 */
 Array.prototype._some = function (callback, thisArg) {
  if (typeof callback !== "function") {
    throw new TypeError(callback + ' is not a function')
  }
  let index = -1 // 索引
  const { length: len } = this // 原数组长度
  while (++index !== len) {
    if (callback.call(thisArg, this[index], index, this)) {
      return true
    }
  }
  return false
}

every

every和some也差不多,只有全部符合条件才返回true,有一项不符合就返回false。

/**
 * @description: every简单实现
 * @param {Function} callback 回调函数 函数中接受(当前遍历的元素,当前遍历索引,当前遍历的数组)三个参数
 * @param {Any} thisArg 传入可以改变callback的this指向
 */
 Array.prototype._every = function (callback, thisArg) {
  if (typeof callback !== "function") {
    throw new TypeError(callback + ' is not a function')
  }
  let index = -1 // 索引
  const { length: len } = this // 原数组长度
  while (++index !== len) {
    if (!callback.call(thisArg, this[index], index, this)) {
      return false
    }
  }
  return true
}

先写这么多,后面再补充其他的。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 添加/移除首端或尾端数组元素 从数组的首端或尾端添加和删除元素的方法: arr.push(...items) ——...
    AaronMIE阅读 319评论 0 0
  • 前言 在开发中,数组的使用场景非常多,平日中也涉及到很多数组的api/相关操作,一直也没有对这块内容进行一块整理总...
    为光pig阅读 640评论 0 9
  • 1. join() 功能: 将数组中的所有元素放入一个字符串。元素是通过指定的分隔符进行分隔的,默认使用’,'号分...
    樑丶阅读 105评论 0 0
  • 数组 知识汇总 前置知识: 数组是一个有序的数据集合,可使用数组名称和索引进行访问。 在JavaScript中数组...
    Daeeman阅读 693评论 1 7
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,606评论 28 53