Arrays provide looping methods

1. The forEach loop

 var arr = ["Zhang"."Bill"."Fifty"];
 arr.forEach(function(item,key,arr){
// item: key (arr[I])
// key: key name (I);
// arr is the original array
 console.log("Key name:",key);
 console.log("Key value:",item);
})
Copy the code

2. Map loop: Copy an array as you loop through it

Map (function(item,key,arr){});
var arr = ["Zhang"."Bill"."Fifty"];
// The returned array is related to the returned value in the callback function
var newarr =   arr.map(function(item,key,arr){
        // console.log(item,key,arr);
        return item;  // Will be recycled back
})
console.log(newarr);//[" zhang SAN "," Li Si "," Wang Wu "]

Copy the code
// Increase all values in the array by 30%
var arr = [100.200.300.400.500];
var newarr = arr.map(function(item){
            return item*1.3;
})
console.log(newarr);
Copy the code

3. Filter cycle: Filter during the cycle

var arr = [1.2.3.4.5.6];
var res = arr.filter(function(item,key,arr){    
     // console.log(item,key,arr);
       return item>4;  // Filter out all values greater than 3 and return them to the new array
})
console.log(res);
Copy the code

4. Some loop: If the array loop returns a value of true then the return value is true; (or)

var arr = [true.false.false.false];
var res =  arr.some(function(item,key,arr){
            return item;  // return item as long as there is a true in the item
            // The final return result is true
 })
console.log(res);//true
Copy the code

5. Every loop returns true when the final result is true.

As long as there is a false, the final result is false.

var arr = [4.5.6.4.1];  
var res =  arr.every(function(item,key){
     // All values in the array are greater than 3;
            return item>3;
})
console.log(res);
Copy the code

Extension of cycle

// The arR object has a forEach method
var arr = ["Zhang"."Bill"."Fifty"];
arr.forEach(function(item,key,arr){
            console.log(item,key,arr);
})
arr.forEach(function(){}); // Zhang SAN (3) [" Zhang SAN ", "Li Si "," Wang Wu "]Li si1 (3) ["Zhang"."Bill"."Fifty"] fifty2 (3) ["Zhang"."Bill"."Fifty"]

Copy the code
//1. Callback: If a function is passed as an argument to another function
// Then this function is called the callback function.
function fn(a) {
            // console.log(a); / / ƒ () {the console. The log (" fn..." ); }
            a();
        }

        var arr = [1.2.3];
        var obj = {name:"Zhang"};
        var myfn = function () {
            console.log("fn...");
        }
        fn(myfn);//fn...
Copy the code
//2. Implement myforEach
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, passing in arguments to the callback function call}}// own forEach
myforEach(arr,function (item,key,arr) {  / / parameter
             console.log("Incoming function execution",item,key,arr);
})
Copy the code
// Array of objects
        var arr = [
        { name: "Zhang".score: 78 }, 
        { name: "Zhang".score: 59 }, 
        { name: "Zhang".score: 89 }, 
        { name: "Zhang".score: 30 }];
        // 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