preface
There are many Array apis. The following lists some common API implementations, which are mainly implemented by referring to Array Polyfill on MDN.
MDN links:Developer.mozilla.org/zh-CN/docs/…
forEach
ForEach is primarily used to iterate over groups of numbers
/ * * *@description: forEach simple implementation *@param {Function} The callback function takes three arguments * (the element currently iterated, the index currently iterated, and the array currently iterated)@param {Any} ThisArg pass can change this for callback to refer to */
Array.prototype._forEach = function (callback, thisArg) {
if (typeofcallback ! = ="function") {
throw new TypeError(callback + ' is not a function')}let index = -1 / / index
const { length: len } = this // Array length
while(++index ! == len) {if (index in this) { // Array subscripts are not contiguous
callback.call(thisArg, this[index], index, this)}}}Copy the code
filter
Filter is used to filter arrays
/ * * *@description: Filter simple implementation *@param {Function} The callback function takes three arguments * (the element currently iterated, the index currently iterated, and the array currently iterated)@param {Any} ThisArg pass can change this for callback to refer to */
Array.prototype._filter = function (callback, thisArg) {
if (typeofcallback ! = ="function") {
throw new TypeError(callback + ' is not a function')}let index = -1, newIdx = 0 // index returns the new array index
const { length: len } = this // The length of the original array
const result = [] // Return a new array
while(++index ! == len) {if (index in this) { // Array subscripts are not contiguous
if (callback.call(thisArg, this[index], index, this)) {
result[newIdx++] = this[index]
}
}
}
return result
}
Copy the code
map
The map callback must have a return value, which returns a new array of callback return values.
/ * * *@description: Map simple implementation *@param {Function} The callback function takes three arguments * (the element currently iterated, the index currently iterated, and the array currently iterated)@param {Any} ThisArg pass can change this to refer to * for callback@return {Array} Returns a new array of callback values */
Array.prototype._map = function (callback, thisArg) {
if (typeofcallback ! = ="function") {
throw new TypeError(callback + ' is not a function')}let index = -1 / / index
const { length: len } = this // Array length
const result = [] // Return a new array
while(++index ! == len) {if (index in this) { // Array subscripts are not contiguous
result[index] = callback.call(thisArg, this[index], index, this)}}return result
}
Copy the code
reduce
Reduce is relatively complex, requiring the determination of whether an initial value is passed in and passing the results of each loop to the next.
/ * * *@description: Simple reduce implementation *@param {Function} The callback function takes three arguments * (the element currently iterated, the index currently iterated, and the array currently iterated)@param {Any} ThisArg pass can change this for callback to refer to */
Array.prototype._reduce = function (. args) {
const callback = args[0] // The callback function
const { length: len } = this // Array length
const { length: argLen } = args // Parameter length
let index = -1 // Array subscript
let result
if (typeofcallback ! = ="function") {
throw new TypeError(callback + ' is not a function')}// there is a second argument, initialValue, as the function's initialValue
if (argLen >= 2) {
result = args[1]}else {
// Find the index of the first item in the array
while(index < len && ! (indexin this)) {
index++
}
// The array is empty and has no initial value
if (index >= len) {
throw new TypeError('Reduce of empty array with no initial value')
}
result = arr[index++]
}
while(++index ! == len) {if (index in this) {
// Pass the result of each return to the next loop
result = callback(result, this[index], index, this)}}return result
}
Copy the code
find
Find returns undefined if it finds none.
/ * * *@description: find simple implementation *@param {Function} The callback function takes three arguments * (the element currently iterated, the index currently iterated, and the array currently iterated)@param {Any} ThisArg pass can change this for callback to refer to */
Array.prototype._find = function (callback, thisArg) {
if (typeofcallback ! = ="function") {
throw new TypeError(callback + ' is not a function')}let index = -1 / / index
const { length: len } = this // The length of the original array
while(++index ! == len) {if (callback.call(thisArg, this[index], index, this)) {
return this[index] // 如果是findIndex则return index}}}Copy the code
some
Some and find implement the same steps except for the return value
/ * * *@description: some simple implementation *@param {Function} The callback function takes three arguments * (the element currently iterated, the index currently iterated, and the array currently iterated)@param {Any} ThisArg pass can change this for callback to refer to */
Array.prototype._some = function (callback, thisArg) {
if (typeofcallback ! = ="function") {
throw new TypeError(callback + ' is not a function')}let index = -1 / / index
const { length: len } = this // The length of the original array
while(++index ! == len) {if (callback.call(thisArg, this[index], index, this)) {
return true}}return false
}
Copy the code
every
Every and some are the same, return true if all criteria are met, and false if one of them is not.
/ * * *@description: every simple implementation *@param {Function} The callback function takes three arguments * (the element currently iterated, the index currently iterated, and the array currently iterated)@param {Any} ThisArg pass can change this for callback to refer to */
Array.prototype._every = function (callback, thisArg) {
if (typeofcallback ! = ="function") {
throw new TypeError(callback + ' is not a function')}let index = -1 / / index
const { length: len } = this // The length of the original array
while(++index ! == len) {if(! callback.call(thisArg,this[index], index, this)) {
return false}}return true
}
Copy the code
Write so much first and fill in the rest later.