Push () and pop()

  1. Stack methods: push() and pop(). A stack is a last-in, first-out data structure, meaning that the latest addition is removed first.
  • The push() method can take any number of arguments, append them one by one to the end of the array, and return the length of the modified array.
  • The pop() method, on the other hand, removes the last item from the end of the array, reducing the length value of the array, and returns the removed item.
var colors = new Array(); // Create an array var count = colors.push("red", "green"); // Push two items alert(count); //2 count = colors.push("black"); // Push another alert(count); //3 var item = colors.pop(); // Get the last alert(item); //"black" alert(colors.length); / / 2Copy the code

Second, queue method

  1. Shift (), which removes the first item in the array and returns that item, while reducing the array length by one.
  • Unshift () does the opposite of shift() : it adds any item to the front of an array and returns the length of the new array.
let arr = [1, 2, 3, 4]; let item = arr.shift(); console.log(item); // 1 let len = arr.unshift('aaa'); console.log(len); // 4 Returns the length of the arrayCopy the code

Reverse () and sort()

  1. Reverse () reverses the order of the array
var values = [1, 2, 3, 4, 5]; values.reverse(); alert(values); / / 5,4,3,2,1Copy the code
  1. Sort () : Sort the items in ascending order — that is, the smallest value is first and the largest is last.
  • The sort() method calls the toString() transformation method for each array item and compares the resulting strings to determine how to sort them. The sort() method compares strings even if every item in the array is a number.
The order of the values in the following example is fine, but the sort() method also changes the order based on the results of the test string. Because the value 5 is less than 10, "10" comes before "5" in string comparisons, the order of the array is changed. Needless to say, this sort of sorting is not optimal in many cases. So the sort() method can take a comparison function as an argument so that we can specify which value comes before which. var values = [0, 1, 5, 10, 15]; values.sort(); alert(values); / / 0,1,10,15,5Copy the code
  • The sort() method takes the function, and the comparison function takes two arguments, returning a negative number if the first argument should come before the second, 0 if the two arguments are equal, and a positive number if the first argument should come after the second.
function compare(value1, value2) { if (value1 < value2) { return -1; } else if (value1 > value2) { return 1; } else { return 0; } } var values = [0, 1, 5, 10, 15]; values.sort(compare); alert(values); / / 0,1,5,10,15Copy the code

4. Operation methods: concat(), slice(), splice()

  1. Concat (). Copy arrays. When parameters are received, they are appended to the end of the copy. The concat() method does not affect the original array.
let arr = ["red", "green", "blue"];
let arr2 = arr.concat();
let arr3 = arr.concat('1', '2')
console.log(arr2, 'arr2');      //  ["red", "green", "blue"] "arr2"
console.log(arr3, 'arr3');      //  ["red", "green", "blue", "1", "2"] "arr3"
console.log(arr, 'arr')         //  ["red", "green", "blue"] "arr"
Copy the code
  1. Slice () intercepts an array from an index location and returns the intercepted contents.
  • Passing an argument intercepts all items at the end of the array starting at a certain point.
  • Pass two parameters: slice(2, 4): indicates the slice from index position 2 to index position 3, excluding index position 4.
  • Pass negative numbers by adding the array length to the arguments. For example, calling slice(-2,-1) on a 5-item array yields the same result as calling slice(3,4). If the end position is less than the start position, an empty array is returned.
  • The slice() method does not affect the original array.
let arr = ["1", "2", "3", 4, 5, 6, 7];
let arr2 = arr.slice(2);
let arr3 = arr.slice(3, 6);
let arr4 = arr.slice(-5, -3);

console.log(arr2, 'arr2');  // ["3", 4, 5, 6, 7] 
console.log(arr3, 'arr3')   // [4, 5, 6]
console.log(arr4, 'arr4')   // ["3", 4] == arr.slice(2, 4)
console.log(arr, 'arr')     // ["1", "2", "3", 4, 5, 6, 7]
Copy the code
  1. The splice() method, which is mainly used to insert items into the middle of an array, can be used in three ways.
  • Delete: You can delete any number of items by specifying two parameters: the location of the first item to be deleted and the number of items to be deleted. For example, splice(0,2) removes the first two items in the array.
let arr = ["1", "2", "3", 4, 5, 6, 7]; // Delete console.log(arr.splice(0, 2)); // ["1", "2"] console.log(arr); // ["3", 4, 5, 6, 7]Copy the code
  • Insert: You can insert any number of items into the specified position by providing three arguments: the starting position, 0 (number of items to delete), and the item to insert. If you want to insert more than one item, you can pass in a fourth, fifth, or any number of items.
let arr = ["1", "2", "3", 4, 5, 6, 7]; // Insert console.log(arr.splice(2, 0, 'aaa')); // [] console.log(arr); // ["1", "2", "aaa", "3", 4, 5, 6, 7]Copy the code
  • Replace: You can insert any number of items into a specified location and delete any number of items simultaneously by specifying three parameters: the start location, the number of items to delete, and any number of items to insert. The number of inserted items need not be equal to the number of deleted items.
let arr = ["1", "2", "3", 4, 5, 6, 7]; // Replace console.log(arr.splice(2, 1, 'BBBB ')); // ["3"] console.log(arr) // ["1", "2", "bbbb", 4, 5, 6, 7]Copy the code

IndexOf () and lastIndexof()

The indexOf() method looks backwards from the beginning of the array (position 0). The lastIndexOf() method starts at the end of the array and returns the index position of the element in the array.

let arr = ["1", "2", "3", 4, 5, 6, 7]; console.log(arr.indexOf(4)); // 3 console.log(arr.lastIndexOf('1')); / / 0Copy the code

Every (), filter(), forEach(), map(), some()

  1. Every () : Runs the given function on each item in the array, and returns true if the function returns true for each item.
let arr = ["1", "2", "3", 4, 5, 6, 7];
let b = arr.every((item) => {
    return item > 2;
})
console.log(b);     // false
Copy the code
  1. Some () : Runs the given function on each item in the array, returning true if the function returns true for any item.
let arr = ["1", "2", "3", 4, 5, 6, 7];
let b = arr.some(item => {
    return item > 2;
});
console.log(b);     // true
Copy the code
  1. Filter () : Returns an array of items that return true by running the given function on each item in the array.
let arr = ["1", "2", "3", 4, 5, 6, 7];
let b = arr.filter((item) => {
    return item > 2;
})
console.log(b);     // ["3", 4, 5, 6, 7]
Copy the code
  1. ForEach () : Runs the given function on each item in the array. This method returns no value. Return, undefined;
let arr = ["1", "2", "3", 4, 5, 6, 7];
let b = arr.forEach(item => {
    return item > 2;
});
console.log(b);     // undefined
Copy the code
  1. Map () : Runs the given function on each item in the array, returning an array of the results of each function call.
let arr = ["1", "2", "3", 4, 5, 6, 7];
let b = arr.map(item => {
    return item + 2;
});
console.log(b);     // ["12", "22", "32", 6, 7, 8, 9]
Copy the code

Vii. Merging (summing) methods: Reduce () and reduceRight()

Both methods accept two parameters: a function called on each item and (optionally) the initial value as the basis for merging. Functions that pass reduce() and reduceRight() take four arguments: the previous value, the current value, the item’s index, and an array object. Any value returned by this function is automatically passed to the next item as the first argument. The first iteration occurs on the second item of the array, so the first argument is the first item of the array, and the second argument is the second item of the array.

  1. Reduce () : Can perform the operation of finding the sum of all values in an array
Var values = [1, 2, 3, 4, 5]; var sum = values.reduce(function(prev, cur, index, array){ return prev + cur; }); alert(sum); / / 15Copy the code
  1. ReduceRight () works similarly, but in reverse.
Var values = [1, 2, 3, 4, 5]; var sum = values.reduceRight(function(prev, cur, index, array){ return prev + cur; }); alert(sum); / / 15Copy the code