Some methods of operating arrays in js

define an array
var numbers= ['1','2','3','4','5'];
1. Delete the tail element
this.numbers.pop();
2. Add elements at the end
this.numbers.push('6');
3. Delete the first element
this.numbers.shift();
4. Add elements to the header
this.numbers.unshift('0');
5. The first parameter is the starting position, and the second parameter is the number of deletions.
 this.numbers.splice(1,3);
6. Sorting
this.numbers.sort();
7. Reversal
this.numbers.reverse();
8. Filter
let result = numbers.filter(function (number){
    
    
          return number > 4
        });
9. Multiply each piece of data by 2
let result2 = numbers.map(function (number){
    
    
          return number * 2;
        });
10. Summation
let total = numbers.reduce(function (preValue,value){
    
    
          return preValue + value;
        },0);

Parameter 1: Callback function. This function has two parameters. Parameter 1 is the value returned by this method in the last loop. Parameter 2 is the element of the current loop. Parameter 2: The
default value of the first parameter of the callback function when called for the first time.
return preValue + value; Function: Add all the values ​​in the array and return a number

Guess you like

Origin blog.csdn.net/dxjren/article/details/128536469