Array of prototype has a lot of kinds of methods above, this article will implement the forEach, filter, map, some, reduce the five methods! Total number: the front end from the advanced to the admission of the dead knock 36 JS handwritten questions

forEach

Array.prototype.myForEach=function(callback, thisArgs){
    if(typeof this! = ='array') {throw new TypeError('this is need array! ')}if(typeofcallback ! = ='function') {throw new TypeError('callback is not a function! ')}let O=Object(this);
    let len=O.length >>> 0;
    let k=0;

    while(k < len){
        if(k inO){ callback.call(thisArgs, O[k], k, O); } k++; }}Copy the code

1. This is the array before foreach

2. Callback is the function to be executed

3.Object(), which returns the corresponding instance based on the passed value

4.>> The two right arrows move right with signs, that is, save the positive and negative values. >>> Three right arrows, unsigned right, convert negative numbers to positive integers and decimals to integers. 0 represents the displacement number. Proving that len makes sense because length can be displayed as modified

5. If (k in O) is used to determine whether the current position of the array is meaningful

ThisArgs = thisArgs; thisArgs = thisArgs; thisArgs = thisArgs; thisArgs = thisArgs; thisArgs = thisArgs

map

Array.prototype.myForEach=function(callback, thisArgs){
    if(typeof this! = ='array') {throw new TypeError('this is need array! ')}if(typeofcallback ! = ='function') {throw new TypeError('callback is not a function! ')}let O=Object(this);
    let len=O.length >>> 0;
    let k=0;
    let res=[];

    while(k < len){
        if(k in O){
            res[k]=callback.call(thisArgs, O[k], k, O);
        }
        k++;
    }

    return res;
}
Copy the code

Only three code changes were made: 1. Map has a return value, set to a RES;

2. Put the return value of the function into the array

3. Return the array

filter

Array.prototype.myForEach=function(callback, thisArgs){
    if(typeof this! = ='array') {throw new TypeError('this is need array! ')}if(typeofcallback ! = ='function') {throw new TypeError('callback is not a function! ')}let O=Object(this);
    let len=O.length >>> 0;
    let k=0;
    let res=[];

    while(k < len){
        if(k in O){
            if(callback.call(thisArgs, O[k], k, O)){
                 res[k]=O[k];
            }
        }
        k++;
    }

    return res;
}
Copy the code

some

Array.prototype.myForEach=function(callback, thisArgs){
    if(typeof this! = ='array') {throw new TypeError('this is need array! ')}if(typeofcallback ! = ='function') {throw new TypeError('callback is not a function! ')}let O=Object(this);
    let len=O.length >>> 0;
    let k=0;

    while(k < len){
        if(k in O){
            if(callback.call(thisArgs, O[k], k, O)){
                 return true;
            }
        }
        k++;
    }

    return false;
}
Copy the code

Some is true and returns true

reduce

Array.prototype.myForEach=function(callback, initValue){
    if(typeof this! = ='array') {throw new TypeError('this is need array! ')}if(typeofcallback ! = ='function') {throw new TypeError('callback is not a function! ')}let O=Object(this);
    let len=O.length >>> 0;
    let k=0;
    let acc;

    if(arguments.length>0){
        acc=initValue;
    }else{
        if( k<len && ! (kin O) ){
            k++;
        }

        if(k>len){
            throw new TypeError('All is empty');
        }

        acc=O[k++];
    }

    while(k < len){
        if(k in O){
            acc=callback(acc, O[k], k, O);
        }
        k++;
    }

    return acc;
}
Copy the code

The implementation of reduce varies greatly. 1. The input parameter is different, and initValue is the initial value

2. Check whether the initial value is passed in. If it is passed in, the first valid value of the array is not passed in

Acc =O[k++], k++ will make k=k+1 in the following code, because the first number, starting from the second

4. Return the number of the last integration

The above carries on the personal understanding interpretation, if has the wrong, please leave a message