美文网首页
js-手写数组常用方法的实现

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

作者: _stan | 来源:发表于2021-06-07 19:21 被阅读0次

前言

数组的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
}

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

相关文章

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

    前言 数组的api比较多,下面主要列举一些常用的api实现,主要是参考MDN上Array的Polyfill来实现的...

  • MJExtension使用的理解

    常用方法: [User objectWithKeyValues:dict] 将字典转为数组 在模型内部实现+ (...

  • 数组

    JS-数组 方法: Array.isArray(obj) : 判断对象是否是Array,是返回true,不是返回f...

  • 手写代码系列

    深拷贝 html转码 手写promise.all 有哪些实现数组扁平化的方法 原生js 运用flat方法 redu...

  • 数组基础

    数组基础 新建数组 数组方法和属性 数组常用方法 数组的遍历方法

  • 数据结构与算法(五),优先队列

    这节总结一下优先队列的常用实现方法。 目录: 1、基本概念 2、基于数组实现的优先队列 2.1、基于有序数组的实现...

  • JS- 数组的方法

    可利用以下几种方式清空数组: 直接赋空值,改变数组长度为0,splice(0), 循环pop/shift直接赋空值...

  • js-数组常用函数

    在这里总结一下JS的数组方法: 1、join()Array.join()方法将数组中所有元素都转化为字符串并连接在...

  • 数组

    数组 数组常用方法 数组去重

  • 数组基础

    数组基础 新建数组 数组方法和属性 数组合并 数组常用方法

网友评论

      本文标题:js-手写数组常用方法的实现

      本文链接:https://www.haomeiwen.com/subject/rklleltx.html