Following up, let’s talk about arrays

An iterative method of an array

  • Sort () ASCII ascending sort(function(a,b){return a-b}) ascending sort(function(a,b){return b-a}) descending sort
  • ForEach (function(v, I,arr){}
  • Map (function(v, I,arr){}) changes the value in the array, returns a new array must return
  • Filter (function(v, I,arr){})
  • Every (function(v, I){}) determines whether every value in the array satisfies the condition
  • Some (function(v, I){}

I’ll wrap a filter method for you here

    // Encapsulate the filter method with cb as a function
        function filter(cb) {
            // go through the number group
            for (var i = 0, arr1 = []; i < arr.length; i++) {
                if (cb(arr[i])) { // call cb, arr[I], return arr[I]<7
                    arr1.push(arr[i])
                }
            }
            console.log(arr1)
        }
        filter(function(v) {
            return v < 7
        })
Copy the code

There is a simple callback function. Here’s what a callback is.

The callback function

Definition :callback: a callback function that takes one function as an argument to another. Parameter: when defining a function, the argument after the function name has no practical meaning. Parameter: the argument after the function when the function is called

For example, to take an easy example,

function buyCar(car) {  
        console.log('Buy one' + car);
         }
    buyCar('car')  
Copy the code

Here’s a simple function call that prints the sports car on the console

If you look at the following, what is car in buycar, is it a function, a function that takes parameters but if you look at the following function buycar when it is called, instead of a value, it takes a function that takes an argument and then passes the function that takes an argument to buycar, The next step is inside buyCar, where car() is called and console.log(‘ Lamborghini ‘) is executed;

function buyCar(car) {
            car()
        }
        buyCar(function() {
            console.log('Lamborghini');
        })
Copy the code

Here, too, is a simple callback function that tests to print lamborghini on the console.

May still be ignorant, but it doesn’t matter, but to know how to use the above array iteration method, remember!! The callback function should be understood naturally.

Now let’s do a difficult example.

Analyzing the following program, the map parameter cb(short for callback) is a function. When we call map, the argument is not a function, and we can see that the argument has a parameter V, where V is the parameter. After passing it to map, cb() appears inside the function, and cb is called again, returning v+1, where arr[I] is the parameter. Return arr[I]+1

function map(cb) {
            // go through the number group
            for (var i = 0; i < arr.length; i++) {
                arr[i] = cb(arr[i]) // call cb with arr[I] as an argument and return arr[I]+1
            }
            console.log(arr);
        }
        map(function(v) { // where v is the parameter
            return v + 1;
        })
Copy the code

This is the map method in the iterative method that encapsulates an array. Changing a value in an array to return a new array must return