Arrays are often used in JavaScript. How to add, delete, or modify an array is very important.

Arr.push (value) adds one or more elements to the end of the array and returns the new length of the array.

Such as:

Let arr = [1, 2, 3, 4, 5] var longth = arr. Push (6, 7); console.log(arr, longth); // array arr changes to arr[1,2,3,4,5,6,7];Copy the code

Length returns the length of the array.

2. Arr.pop () removes the element with the largest index value and returns the deleted element.

Let arr = [1, 2, 3, 4, 5] var delElement = arr. Pop (); console.log(arr, delElement); //arr has the value [1,2,3,4] and delElement has the value 5Copy the code

Arr.pop () has the same effect as arr.length, but arr.length does not return a value;

Inserts elements at the head of the array

unshift(value); Adds one or more elements to the header of the array and returns the new length of the array

Let arr=[1,2,3,4,5] var length= unshift(0); console.log(arr,length); //arr is [0,1,2,3,4,5]; The value of length is the changed length of the array 6;Copy the code

4.shift(); Deletes the element with index 0 and returns the deleted element

Let arr=[1,2,3,4,5] var element = unshift(); console.log(arr, delElement); //arr is [2,3,4,5]; The value of delElement is the deleted element 1;Copy the code

5. Concat () merges arrays or elements and returns a new array, unchanged

Let arr = [1, 2, 3, 4, 5] let newArr = arr. Concat ((June), 9, 10); console.log(newArr,arr); // the newArr value is [1,2,3,4,5,6,7,8,9,10]; //arr is the same as before [1,2,3,4,5];Copy the code

Concat () can also copy a new array;

let copyArr=arr.concat(); //copyArr has the same value as arr

6.splice(); Add or remove elements at any location, return the value removed or replaced, or return an empty array if not;

The splice() method modifies the value of the array; When there is only one value, it is removed from the current position to the end of the array

Let arr = [1, 2, 3, 4, 5]; let num1=arr.splice(1) console.log(num1; Arr) / / num = 5-tetrafluorobenzoic [2]; arr=[1];Copy the code

If there are two values, the first value is the location of the deletion and the second value is the number of the deletion.

Let arr = [1, 2, 3, 4, 5]; Let num1=arr.splice(2,3)// Delete 3 elements from index 2 console.log(num1; arr); / / num1 = (three, four, five), arr = [1, 2]Copy the code

When there are three or more values, the first value is the position of the element to be inserted, the second value is the number of substitutions, and the following values are the new elements to be inserted.

Let arr = [1, 2, 3, 4, 5]; Let num2 = arr. Splice,1,6,7,8 (2); // if the second value is 0, insert 6,7,8 instead of replacing; console.log(num2; arr); // Num2 =[3]; Arr =,2,6,7,8,4,5 [1]Copy the code

Intercepts the contents of a copy array at the specified location

Slice (start position, end position) The second parameter is not written to the tail, can only be cut from the front to the back; The value returned is a new array of the intercepted content;

let copyArr=arr.slice(); // Slice () or slice(0) can copy arrays; Let arr = [1, 2, 3, 4, 5]; Let newArr = arr. Slice (1, 3); // Truncate values from index 1 to index 3(excluding index 3); The console. The log (newArr, arr); / / newArr = [2, 3]; Arr = [1, 2, 3, 4, 5];Copy the code

The slice() method does not change the value of the original array, specifying the character concatenation string;

7.join(); Each element of the array is returned as a new string with the specified concatenation;

Let arr = [1, 2, 3, 4, 5]; Let newArr=arr.join()// Join console.log(newArr) with a comma by default; / / newArr = 1, 2, 3, 4, 5; // If the concatenation is an empty string, console.log(arr.join(" ")) will be seamlessly joined; // The output is 12345;Copy the code

Sort the array

8. Sort () sorts the array (ascending), returns the new array, and changes the original array;

Let arr =,3,5,1,4 [2]; let newArr=arr.sort(); The console. The log (newArr, arr); / / newArr = [1, 2, 3, 4, 5]; Arr r = [1, 2, 3, 4, 5]Copy the code

Invert the array

9. Reverse (); You can reverse the array and return a new array, changing the original array as well;

Let arr = [1, 2, 3, 4, 5]; let newArr=arr. reverse(); The console. The log (newArr, arr); / / newArr =,4,3,2,1 [5]; Arr =,4,3,2,1 [5];Copy the code

Above is the common method of array!