Array.prototype.forEach
- forEach用于遍历数组,该方法接收两个参数
- 第一个参数是一个函数:该函数会有三个参数,value,index,arr。
- 第二个参数:可以用于改变第一个参数函数里面的this。
- 该函数没有返回值
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一般用于对数组进行求和,该方法接收两个参数
- 第一个参数必须是一个函数:该函数会有四个参数,上一次求和的值,当前数组项的值,当前下标,整个数组
- 第二个参数是设置第一次的上一次求和值,默认是函数的第一项,也就是说如果不传该参数,那么函数会从第二项开始进行相加。
- 该函数的返回值就是求和之后的值。
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 = arr[0]
i = 1
} else {
sum = initialValue
}
for (; i < arr.length; i++) {
callback(sum, arr[i], i, arr)
sum += arr[i]
}
return sum
}
Array.prototype.map
- 该方法可以对数组的每一项进行处理,并返回一个处理后的函数
- 该方法接收两个参数,和forEach一样
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一致
- 该方法返回一个布尔值
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)) {
continue
}
return false
}
return true
}
Array.prototype.some
- 该方法用于判断数组中是否有一项满足条件,如果满足条件立即返回true,如果所有都不满足,返回false。
- 该方法的参数和forEach一致
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一致。
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++) {
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
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
}
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
}
其他手写函数以及代码