Array iteration method and its reconstruction

1. Iterative method

1.every(function(item,index,arr){},this object) 
• the first parameter is a fixed function that will be executed by each element in the array
​
• the second parameter refers to this in the previous function -- the second parameter refers to any data type
​
• judge whether all array elements meet a certain condition. If one item does not meet, it will directly jump out of the current judgment and comply with the short circuit principle. filter() does not detect empty arrays and does not change the original array.
2.some(function(item,index,arr){},this object)
• the first parameter (similar to the every method)
​
• second parameter (similar to the every method)
​
• judge whether one of the array elements meets the condition and one item meets the condition, jump out of the judgment and return true, which conforms to the short circuit principle. filter() does not detect empty arrays and does not change the original array.
3.filter(function(item,index,arr){},this object) 
• the first parameter (similar to the every method) 
​ 
• second parameter (similar to the every method)

• filter out the elements that meet a certain condition to form a new array. filter() does not detect empty arrays and does not change the original array.
4.map(function(item,index,arr){},this object) 
• the first parameter (similar to the every method)
​
• second parameter (similar to the every method)

• map a part of the element to form a new array. map() does not detect empty arrays and does not change the original array.
5.forEach(function(item,index,arr){},this object)
• the first parameter (similar to the every method)
​
• second parameter (similar to the every method)

• the forEach method is used to call each element of the array and pass the element to the callback function. forEach() does not execute callback functions for empty arrays. 
Note: in the iterative method, there is no second parameter this pointing to the global object. The global object in node is global. The global object in html document is window. There is a second parameter this pointing to the second parameter. The second parameter can be any data type

2. Reconstruction iteration method

(1) If myevery is dissatisfied, it will directly return false and will not continue to judge later. If all are satisfied, return true

Array.prototype.myEvery=function(fun,obj){
        for(var i=0;i<this.length;i++){
            if(!(obj?fun.bind(obj)(this[i]):fun(this[i]))){
            return false
            } //If there is a second parameter, you need to modify this point
            //if(!fun(this[i])){
            //    return false
            //}--- logical operation without second parameter
        }
        return true
    };
    var arr=[1,2,3,4,5];
    var result=arr.myEvery(function(item,index,arr){
        console.log(this) //There is a second parameter, and this points to the second parameter
        return item>1  //If a condition is not satisfied, false is returned directly
    },{name:'zhangsan'}) //The second parameter can be any data type)
    console.log(result)

(2)mysome returns true directly if one of the items meets the requirements. It will not be judged later, but return true directly

Array.prototype.mySome=function(fun,obj){
    for(var i=0;i<this.length;i++){
        if((obj?fun.bind(obj)(this[i]):fun(this[i]))){
            return true
        }
    }
        return false
    };
    var arr=[1,2,3,4,5];
    var result=arr.mySome(function(item,index,arr){
        console.log(this) //There is a second parameter, and this points to the second parameter; Here, this is printed twice. First, it is printed once before there are no execution conditions. Then, if the array item 1 does not meet the conditions, it will be printed again. If the array item 2 meets the conditions, it will directly jump out and judge not to print
        return item>1  //If an item meets the requirements, it will directly return true
    },{name:'zhangsan'}) //The second parameter can be any data type)

(3) The mymap map operates on each array item and returns a new array after the operation

Array.prototype.myMap = function (fun, obj) {
    var result = [];
    for (i = 0; i < this.length; i++) {
        //obj?test():test2()
        result.push(obj ? fun.bind(obj)(this[i]) : fun(this[i]))
    }
    return result
    }
    var arr = [1, 2, 3, 4, 5];
    var result = arr.myMap(function (item, index, arr) {
        console.log(this)//There is no second parameter this, which points to the global object node. In the node, it points to the window in the global html document. / / there is a second parameter this, which points to the second parameter
        return item + 1
    }, { name: 'zhangsan' })
    console.log(result)

(4)myFilter returns an array containing all qualified elements. If there are no qualified elements, an empty array is returned.

Array.prototype.myFilter=function(fun,obj){
    var result=[];
    for(i=0;i<this.length;i++){
        if(obj?fun.bind(obj)(this[i]):fun(this[i])){
            result.push(this[i])
        }
    }
        return result
    }   
​
    var arr=[1,2,3,4,5];
    var result=arr.myFilter(function(item,index,arr){
        console.log(this)//Global / / obj / / print five times
        return item>2 
    },{name:'zhangsan'})
    console.log(result)

(5)myForEach

Array.prototype.myForEach=function(fun,obj){
  for(var i=0;i<this.length;i++){
      obj ? fun.bind(obj)(this[i],i,this) : fun(this[i],i,this)
  }
}
var arr=[1,2,3];
console.log(arr);
var result=arr.myForEach(function(item,index,arr){
  console.log(this);
  console.log(item,index,arr)
},{name:'zhangsan'});

Tags: Javascript

Posted on Thu, 02 Sep 2021 03:35:47 -0400 by PunjabHaker