//1. Push (), which takes any number of arguments and adds them to the end of the array
var a = [‘A’, ‘B’];
a.push(‘C’);
a // [‘A’, ‘B’, ‘C’]
//2.pop(), takes the last element and returns it
var a = [‘A’, ‘B’];
a.pop(); // B
a // [‘A’]
//3. Shift takes out the first element and returns
var a = [‘A’, ‘B’];
a.shift(); // A
a // [‘B’]
//4. Unshift, add element to header
var a = [‘B’];
a.unshift(‘A’);
a // [‘A’, ‘B’]
//5.splice() can add elements and delete and replace elements. // Splice () can pass multiple arguments. The first argument is the starting position in the array (index in the array, including the starting position). The second argument represents the number of elements to be removed (0 when adding), followed by the element to be added (multiple arguments can be passed)
var a = [‘A’,’B’,’C’,’D’,’E’,’F’]
A.s plice (2, 2, “B”, “B”)
a // [ ‘A’, ‘B’, ‘B’, ‘b’, ‘E’, ‘F’ ]
// 6. The indexOf method takes two arguments: Var a = [‘ a ‘,’B’,’C’,’D’,’ a ‘,’F’] var a = [‘ a ‘,’B’,’C’,’D’,’ a ‘,’F’]
// var num = a.indexOf(“A”,2)
// num // -1
var num = a.indexOf(“A”)
num // 0
// 7.lastIndexof () This method takes two arguments: the first argument is the item to be searched, and the second argument (optionally) is to look forward from the index, or backward if not passed, and returns the index to be searched, or -1 if not found
var a = [‘A’,’B’,’C’,’D’,’A’,’F’]
// var num = a.lastIndexOf(“A”,2)
// num // 0
var num = a.lastIndexOf(“A”)
num // 4
// 8.reverse () reverses the entire array without arguments
var a = [‘A’,’B’,’C’,’D’,’A’,’F’]
a.reverse()
a //[ ‘F’, ‘A’, ‘D’, ‘C’, ‘B’, ‘A’ ]
// 9.sort () accepts a callback function (optional) that returns two comparisons to determine whether the result is in ascending or descending order.
var a = [‘A’,’B’,’C’,’D’,’A’,’F’]
a.sort((a, b) => {
If (a < = b) {/ / by some sorting standard comparison, a less than b, if it is a simple comparison of digital can directly use (a, b) = a – > b, if is the character doesn’t work
return -1 ;
}
if (a > b ) {
return 1;
}
})
a // [ ‘A’, ‘A’, ‘B’, ‘C’, ‘D’, ‘F’ ]
// 10.foreach () passes a callback function that takes three arguments
var a = [‘A’,’B’,’C’,’D’,’A’,’F’]
a.forEach(function(item,index,a) {
a[index] = item *2; // Change the array here
});