The callback function

1. Callback: A function that is passed as an argument to another function is called a callback.

function fn(cb){
                cb(1.2[1.2.3]); // The cb callback function can also be executed when fn calls
            }
fn(function(item,key,arr){
                console.log("New function"."The argument passed in is",item,key,arr);
}); // The argument passed to the new function is 1 2 (2) [1, 2]
// This call takes the function as an argument to the fn function above
Copy the code

Implement a myForEach method to achieve the built-in forEach effect

 var arr = ["Zhang"."Bill"."Fifty"];
 function  myforEach(arr,fn) {
            // arr is an array
            Function (item,key,arr) {}
            // loop array
            for(var i=0; i<arr.length; i++){ fn(arr[i],i,arr);// is called three times, and the callback function calls the argument passed in}}// own forEach
myforEach(arr,function (item,key,arr) {  / / parameter
                console.log("Incoming function execution",item,key,arr);
        })
Copy the code

How to loop through the array of objects?

 var arr=[
                {name:"1".score:80},
                {name:"A second".score:60},
                {name:"Zhang".score:67},
                {name:"Zhang four".score:56},
                {name:"Zhang five".score:67},
                {name:"Zhang six".score:43},]// How to select all the students who have passed the exam
var newarr = arr.filter(function(item){
                 console.log(item);  // Is for each object
                 return item.score>=60;// Get a score in each object greater than or equal to 60;

})
 console.log(newarr);// Select all passing students
Copy the code

4, implement a myEvery method to achieve the built-in every effect

1. The result of every is related to the return value of the callback function, which is the result of the function execution. Return results cannot have false, if there is a false, then the final result is false, which is the ampers& (and) relation of all results.

//1. Is a && relationship
var arr = [3.4.5];
var res = true;
        for(var i=0; i<arr.length; i++){ res = res && arr[i];// arr[0] && arr[1] && arr[2]
        }
        console.log(res);  // all values are true, if any of them are false then the result is false
            var res=ture;
            for(var i=0; i<arr.length; i++){ res=res&arr[i] }console.log(res);
Copy the code
// 2. Get the result of the callback function
        var arr = [2.4.5];
        function  myEvery(arr,fn) {
            var result = true;
            for(var i=0; i<arr.length; i++){// key is I; The item is arr [I];
                // console.log(i,arr[i])
                 var res =  fn(arr[i],i,arr);  // This is the result of the callback function, the return value of the incoming callback
            // console.log(" result ",res);
                 result = res && result;
            }
            return result;
        }

       var res =  myEvery(arr,function (item,key,arr) {
            console.log("Executed.",item,key,arr);
            return item>1;// The return result depends on the return value. If the determination is greater than 1 here, it is true. If one is flase, the result is flase.
        });
        console.log(res);// Execute 2 0 (3) [2, 4, 5]
                         // Execute 4 1 (2, 4, 5)
                        // Execute 5 2 (3) [2, 4, 5]
                        //true
                           
Copy the code