Summary of some methods of array Array of

  • 2021-07-21 07:29:02
  • OfStack

Overview of array object properties and methods:

1 > arr. push () adds a parameter to the end of the array and returns the length of the new array

2 > arr. unshift () adds a parameter to the beginning of the array and returns the length of the new array

3 > arr. shift () Deletes the first bit of the array and returns the deleted number

4 > arr. pop () Deletes the last 1 bit of the array and returns the deleted number

5 > arr. concat () merges the numbers in the parameter after the original array, does not change the original array, and returns a new array

6 > arr. reverse () flips the array, changing the original array

7 > arr. sort () Defines how arrays are sorted
arr.sort(function (a,b){
return a-b;
});

If it is a-b, it is sorted from small to large; If it is b-a, it is sorted from big to small

8 > arr. slice (startIndex, endIndex) truncates an array, does not change the original array, and returns a new array

Interception starts at index startIndex and ends at index endIndex. It can be fetched at startIndex, but not at endIndex.

If the endIndex parameter is not written, it is intercepted to the end by default.

9 > arr. splice () Deleting an array changes the original array

When there are two parameters, the first is the starting position of the deletion array, and the second is the number of deletions, starting from the starting position;

When there are 3 parameters or more, the third and later parameters are added to the front of the array.

10 > arr. join () splices the array into a string with the passed-in parameters without changing the original array.

11 > arr. indexOf () returns index value if a match is found-1 if not found


Related articles: