JS手写题-常见数组方法

976 阅读2分钟

Array.prototype.forEach

  • forEach用于遍历数组,该方法接收两个参数
  • 第一个参数是一个函数:该函数会有三个参数,value,index,arr。
  • 第二个参数:可以用于改变第一个参数函数里面的this。
  • 该函数没有返回值

如果不了解forEach的使用,点击我查看forEach的详细介绍

// 手写forEach
Array.prototype.myForEach = function (callback, thisArg) {
    // 获取要遍历的数组
    const arr = this
    
    // 判断传入的第一个参数是否为函数,如果抛出异常
    if (!typeof callback !== 'function') {
        throw new TypeError(`${callback} is not a function`)
    }
    
    for (let i = 0; i < arr.length; i++) {
        callback.call(thisArg, arr[i], i, arr)
    }
}

Array.prototype.reduce

  • reduce一般用于对数组进行求和,该方法接收两个参数
  • 第一个参数必须是一个函数:该函数会有四个参数,上一次求和的值,当前数组项的值,当前下标,整个数组
  • 第二个参数是设置第一次的上一次求和值,默认是函数的第一项,也就是说如果不传该参数,那么函数会从第二项开始进行相加
  • 该函数的返回值就是求和之后的值

如果不了解reduce的使用,点击我查看reduce的详细介绍

// 手写reduce函数
Array.prototype.myReduce = function (callback, initialValue) {
    // 判断传入的第一个参数是否为函数
    if (typeof callback !== 'function') {
        throw new TypeError(`${callback} is not a function`)
    }
    
    const arr = this
    let sum = 0
    let i = 0
    
    // 判断是否有传递第二个参数
    if (arguments.length === 1) { 
        // 没有传递第二个参数, 将sum设置为数组第一项,并且循环从第二项开始
        sum = arr[0]
        i = 1
    } else {
        // 有传递第二个参数,将sum设置传进来的initialValu
        sum = initialValue
    }
    
    for (; i < arr.length; i++) {
        callback(sum, arr[i], i, arr)
        sum += arr[i]
    }
    
    return sum
}

Array.prototype.map

  • 该方法可以对数组的每一项进行处理,并返回一个处理后的函数
  • 该方法接收两个参数,和forEach一样

如果不了解map的使用,点击我查看map的详细介绍

Array.prototype.myMap = function (callback, thisArg) {
    // 判断传入的第一个参数是否为函数
    if (typeof callback !== 'function') {
        throw new TypeError(`${callback} is not a function`)
    }
    
    const arr = this
    const mapArr = []
    
    for (let i = 0; i < arr.length; i++) {
        newArr.push(callback.call(thisArh, arr[i], i, arr))
    }
    
    return newArr
} 

Array.prototype.every

  • 该方法可以用于数组中的所有项都满足某一个条件,当全部满足时,会返回false,当有一个不满足时,会直接返回false,后面如果还有其他数组项的也不会进行遍历了
  • 该方法的参数和forEach一致
  • 该方法返回一个布尔值

如果不了解every的使用,点击我查看every的详细介绍

Array.prototype.myEvery = function (callback, thisArg) {
    // 判断传入的第一个参数是否为函数
    if (typeof callback !== 'function') {
        throw new TypeError(`${callback} is not a function`)
    }
    
    const arr = this
    
    for (let i = 0; i < arr.length; i++) {
        if (callback.call(thisArg, arr[i], i, arr)) {  // 如果当前项返回true,那么继续遍历
            continue
        }
        return false
    }
    // 如果到这里,证明前面所有的项都满足条件,返回true
    return true
}

Array.prototype.some

  • 该方法用于判断数组中是否有一项满足条件,如果满足条件立即返回true,如果所有都不满足,返回false。
  • 该方法的参数和forEach一致

如果不了解some的使用,点击我查看some的详细介绍

Array.prototype.mySome = function (callback, thisArg) {
  // 判断传入的第一个参数是否为函数
  if (typeof callback !== 'function') {
    throw new TypeError(`${callback} is not a function`)
  }
  
  const arr = this

  for (let i = 0; i < arr.length; i++) {
    if (callback.call(thisArg, arr[i], i, arr)) {
      return true
    }
  }
  return false
}

Array.prototype.filter

  • 该方法用于过滤数组,会返回一个新数组。当满足当前项满足条件时,该项会被加入到新数组中,当不满足条件时,该项不会被加入到新数组中。
  • 该方法接收的参数和forEach一致。

如果不了解filter的使用,点击我查看filter的详细介绍

Array.prototype.myFilter = function (callback, thisArg) {
  // 判断传入的第一个参数是否为函数
  if (typeof callback !== 'function') {
    throw new TypeError(`${callback} is not a function`)
  }

  const arr = this
  const filterArr = []

  for (let i = 0; i < arr.length; i++) {
    // 当满足条件时,将该项加入到filterArr中
    if (callback.call(thisArg, arr[i], i, arr)) {
      filterArr.push(arr[i])
    }
  }
  return filterArr
}

Array.prototype.find和Array.prototype.findIndex

  • 这两个函数接收的参数和forEach一致
  • find函数用于找到数组中第一个满足条件的元素,该函数会返回第一个满足条件的元素,如果没有找到满足条件的,就返回undefined。
  • findIndex函数和find函数类似,只不过它返回的是第一个满足元素的下标,如果没有找到的话返回-1

如果不了解find的使用,点击我查看find的详细介绍

如果不了解findIndex的使用,点击我查看findIndex的详细介绍

// 实现find
Array.prototype.myFind = function (callback, thisArg) {
  // 判断传入的第一个参数是否为函数
  if (typeof callback !== 'function') {
    throw new TypeError(`${callback} is not a function`)
  }

  const arr = this

  for (let i = 0; i < arr.length; i++) {
    if (callback.call(thisArg, arr[i], i, arr)) {
      return arr[i]
    }
  }
  return undefined
}

// 实现findIndex
Array.prototype.myFindIndex = function (callback, thisArg) {
  // 判断传入的第一个参数是否为函数
  if (typeof callback !== 'function') {
    throw new TypeError(`${callback} is not a function`)
  }

  const arr = this

  for (let i = 0; i < arr.length; i++) {
    if (callback.call(thisArg, arr[i], i, arr)) {
      return i
    }
  }
  return -1
}

其他手写函数以及代码

js手写题-防抖节流

js手写题代码