Array.prototype.forEach
- ForEach is used to iterate over a group of numbers, which takes two arguments
- The first argument is a function that takes three arguments, value, index, and arr.
- The second argument: can be used to change this in the first argument function.
- This function has no return value
If you don’t know how to use forEach, click on me for a detailed introduction to forEach
/ / handwritten forEach
Array.prototype.myForEach = function (callback, thisArg) {
// Get the array to iterate over
const arr = this
// Determine if the first argument passed in is a function, if an exception is thrown
if (!typeofcallback ! = ='function') {
throw new TypeError(`${callback} is not a function`)}for (let i = 0; i < arr.length; i++) {
callback.call(thisArg, arr[i], i, arr)
}
}
Copy the code
Array.prototype.reduce
- Reduce is typically used to sum an array, and this method takes two arguments
- The first argument must be a function: the function takes four arguments, the value of the last summation, the value of the current array item, the current index, and the entire array
- The second argument sets the value of the previous sum for the first time. The default is the first term of the function, meaning that if this argument is not passed, the function will start adding from the second term.
- The return value of this function is the sum.
If you are not familiar with the use of Reduce, click on me to see the detailed introduction of Reduce
// Write reduce functions
Array.prototype.myReduce = function (callback, initialValue) {
// Check whether the first argument passed in is a function
if (typeofcallback ! = ='function') {
throw new TypeError(`${callback} is not a function`)}const arr = this
let sum = 0
let i = 0
// Check whether the second argument is passed
if (arguments.length === 1) {
// Without passing the second argument, sum is set to the first item in the array, and the loop starts from the second item
sum = arr[0]
i = 1
} else {
// There is initialValu to pass in the sum setting as the second argument
sum = initialValue
}
for (; i < arr.length; i++) {
callback(sum, arr[i], i, arr)
sum += arr[i]
}
return sum
}
Copy the code
Array.prototype.map
- This method processes each item of the array and returns a processed function
- This method takes two arguments, just like forEach
If you are not familiar with the use of Map, click me for a detailed description of map
Array.prototype.myMap = function (callback, thisArg) {
// Check whether the first argument passed in is a function
if (typeofcallback ! = ='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
}
Copy the code
Array.prototype.every
- This method can be used when all items in an array satisfy a certain condition. If all items in an array satisfy a certain condition, false will be returned. If one item does not satisfy a condition, false will be returned
- This method has the same parameters as forEach
- This method returns a Boolean value
If you are not familiar with the use of every, click on me to see the details of every
Array.prototype.myEvery = function (callback, thisArg) {
// Check whether the first argument passed in is a function
if (typeofcallback ! = ='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)) { // If the current item returns true, continue traversing
continue
}
return false
}
// Return true if all of the previous entries satisfy the condition
return true
}
Copy the code
Array.prototype.some
- This method is used to determine if any of the items in the array meet the criteria, and returns true immediately if the criteria are met, or false if all are not met.
- This method has the same parameters as forEach
If you’re not familiar with the use of some, click on me for a full description of some
Array.prototype.mySome = function (callback, thisArg) {
// Check whether the first argument passed in is a function
if (typeofcallback ! = ='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
}
Copy the code
Array.prototype.filter
- This method is used to filter an array and returns a new array. If the current item satisfies the condition, the item is added to the new array; if the condition is not satisfied, the item is not added to the new array.
- This method receives the same parameters as forEach.
If you are not familiar with the use of Filter, click me to view the detailed introduction of Filter
Array.prototype.myFilter = function (callback, thisArg) {
// Check whether the first argument passed in is a function
if (typeofcallback ! = ='function') {
throw new TypeError(`${callback} is not a function`)}const arr = this
const filterArr = []
for (let i = 0; i < arr.length; i++) {
// Add the item to filterArr when the condition is met
if (callback.call(thisArg, arr[i], i, arr)) {
filterArr.push(arr[i])
}
}
return filterArr
}
Copy the code
Array. The prototype. The find and Array. Prototype. FindIndex
- These two functions take the same arguments as forEach
- The find function is used to find the first element in an array that meets the criteria. This function returns the first element that meets the criteria, or undefined if none is found.
- The findIndex function is similar to find except that it returns the first index that satisfies the element, or -1 if none is found
If you’re not familiar with the use of find, click me for a full description of find
If you are not familiar with the use of findIndex, click on me for a detailed description of findIndex
/ / the find
Array.prototype.myFind = function (callback, thisArg) {
// Check whether the first argument passed in is a function
if (typeofcallback ! = ='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
}
/ / implementation findIndex
Array.prototype.myFindIndex = function (callback, thisArg) {
// Check whether the first argument passed in is a function
if (typeofcallback ! = ='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
}
Copy the code