一、forEach
Array.prototype.forEach2 = function (callback, thisCtx) {
if (typeof callback !== 'function') {
throw `${callback} is not a function`
}
const length = this.length
let i = 0
while (i < length) {
// Deleted, the newly added element index i is not in the array, so it will not be accessed
if (this.hasOwnProperty(i)) {
callback.call(thisCtx, this[ i ], i, this)
}
i++
}
}
二、map
Array.prototype.map2 = function (callback, thisCtx) {
if (typeof callback !== 'function') {
throw `${callback} is not a function`
}
const length = this.length
let i = 0
// The return value of the map method is a new array
let newArray = []
while (i < length) {
// Deleted and uninitialized values will not be accessed
if (this.hasOwnProperty(i)) {
newArray.push(callback.call(thisCtx, this[ i ], i, this))
}
i++
}
// Return new array
return newArray
}
三、every
Array.prototype.every2 = function (callback, thisCtx) {
if (typeof callback !== 'function') {
throw `${callback} is not a function`
}
const length = this.length
let i = 0
// If the length of the array is 0, the while loop will not be entered
while (i < length) {
// False will be returned as long as a value does not conform to the judgment of callback
if (this.hasOwnProperty(i) && !callback.call(thisCtx, this[ i ], i, this)) {
return false
}
i++
}
return true
}
四、some
Array.prototype.some2 = function (callback, thisCtx) {
if (typeof callback !== 'function') {
throw `${callback} is not a function`
}
const length = this.length
let i = 0
while (i < length) {
// Returns true if any element meets the callback condition
if (this.hasOwnProperty(i) && callback.call(thisCtx, this[ i ], i, this)) {
return true
}
i++
}
return false
}
五、filter
Array.prototype.filter2 = function (callback, thisCtx) {
if (typeof callback !== 'function') {
throw `${callback} is not a function`
}
const length = this.length
// The return value will be a new array
let newArray = []
let i = 0
while (i < length) {
if (this.hasOwnProperty(i) && callback.call(thisCtx, this[ i ], i, this)) {
newArray.push(this[ i ])
}
i++
}
return newArray
}
六、reduce
Array.prototype.reduce2 = function (callback, initValue) {
if (typeof callback !== 'function') {
throw `${callback} is not a function`
}
let pre = initValue
let i = 0
const length = this.length
// When the initial value is not passed, use the first value of the array as the initial value
if (typeof pre === 'undefined') {
pre = this[0]
i = 1
}
while (i < length) {
if (this.hasOwnProperty(i)) {
pre = callback(pre, this[ i ], i, this)
}
i++
}
return pre
}
七、reduceRight
Array.prototype.reduceRight2 = function (callback, initValue) {
if (typeof callback !== 'function') {
throw `${callback} is not a function`
}
let pre = initValue
const length = this.length
// Start with the last element
let i = length - 1
// If no initial value is passed, the last element is taken as the initial value
if (typeof pre === 'undefined') {
pre = this[i]
i--
}
while (i >= 0) {
if (this.hasOwnProperty(i)) {
pre = callback(pre, this[ i ], i, this)
}
i--
}
return pre
}
八、find
Array.prototype.find2 = function (callback, thisCtx) {
if (typeof callback !== 'function') {
throw `${callback} is not a function`
}
const length = this.length
let i = 0
while (i < length) {
const value = this[ i ]
// As long as there is an element that matches the logic of the callback function, the element value is returned
if (callback.call(thisCtx, value, i, this)) {
return value
}
i++
}
// otherwise return undefined
return undefined
}
九、findIndex
Array.prototype.findIndex2 = function (callback, thisCtx) {
if (typeof callback !== 'function') {
throw `${callback} is not a function`
}
const length = this.length
let i = 0
while (i < length) {
// Return index i that conforms to callback logic
if (callback.call(thisCtx, this[ i ], i, this)) {
return i
}
i++
}
return -1
}
十、indexOf
Array.prototype.indexOf2 = function (targetEle, fromIndex) {
const length = this.length
fromIndex = +fromIndex || 0
// If the array is empty or the search starts from a place greater than or equal to the length of the array, it will directly return -1
if (length === 0 || fromIndex >= length) {
return -1
}
/*
1. Search elements from fromIndex
2. Use it directly when fromindex is greater than 0
3. If it is less than 0, first subtract the absolute value of fromIndex from the length. If it is still less than 0, take 0 directly
*/
let i = Math.max(fromIndex >= 0 ? fromIndex : length - Math.abs(fromIndex), 0)
while (i < length) {
// element in the array and equal to targetEle
if (this.hasOwnProperty(i) && targetEle === this[ i ]) {
return i
}
i++
}
return -1
}
以上就是十个数组方法的实现!!!