An array of

1. Overview of arrays

An array is a collection that represents an ordered series of data

Said method

[] : An array whenever you see square brackets

  • Each element in the array is separated by a comma
  • An item in the array can be obtained by subscript (index starts at 0) and can be assigned
var arr = [1.'2'.'hello'.true.5];
console.log(arr[3]);
// true
Copy the code

2. Array length

Array data type: Object reference type

Reference types typically have their own attributes (length) and methods (push() pop()), and are invoked by means of dots (.)

The length of the array is represented by the length attribute, which is called directly

Last item in array: arr[arr. Lenght-1]

var arr = [1.2.3.6.8.5.9.0];
// Outputs the length of the array
console.log(arr.length); 
/ / 8

// Outputs the last item of the array
console.log(arr[arr.length-1]);
/ / 0

// Assign to an item in the array
arr[2] = 4;
console.log(arr[2]);
/ / 4

// Outputs an item that does not exist in the array
console.log(arr[9]);
// undefined

// Increase the length of the array
arr[20] = 20
console.log(arr[20]);
/ / 20
console.log(arr.length);
/ / 21

// Reduce the size of the array
arr.length = 3;
console.log(arr);
// [1, 2, 4]
Copy the code

3. Array traversal

Traversal: To get each item in an array by some method

  • The for loop

    var arr = [1.2.'hello'.' '.function(){}, 12.23.null.false];
    // The for loop iterates through each item in the output array
    for (let i = 0; i < arr.length; i++) {
      const element = arr[i];
      console.log(element);
    }
    // 1 2 你好 '' [Function] 12 23 null false
    Copy the code
  • The map method

    var arr = [1.2.'hello'.' '.function(){}, 12.23.null.false];
    // The map method iterates over each item in the output
    arr.map(item= > {
      console.log(item);
    })
    // 1 2 你好 '' [Function] 12 23 null false
    Copy the code

Case: arr = [2,4,6,7,8]; Take the factorial of each term in the array and sum it up.

var arr = [2.4];

var sum = 0;
for (let i = 0; i < arr.length; i++) {
  sum += jiecheng(arr[i])
}
console.log(sum);
/ / 26

function jiecheng(x) {
  var cheng = 1
  for (let i = 1; i <= x; i++) {
    cheng *= i
  }
  return cheng;
}
Copy the code

4. Start and end operations on arrays

Pop () : Deletes the last item of the array with no argument and returns the value of the deleted item

Push () : Add data parameters to the end of the array: The data items to be added are separated by commas to add the length of the array

Shift () : Deletes the first item of the array. No argument returns the value of the deleted item

Unshift () : Adds data arguments to the beginning of the array: The data items to be added are separated by commas to add the length of the array

var arr = [1.2.3.4.5]
// pop() deletes the last item and returns the deleted value
console.log(arr.pop());
/ / 5

// Add data to the end of push() returns the length of the array
console.log(arr.push(5.6[7.8]));
/ / 7
console.log(arr);
// [1, 2, 3, 4, 5, 6, [7, 8]]

// pop() deletes the first item and returns the deleted value
console.log(arr.shift());
// add data at the beginning of unshift() returns the length of the added array
console.log(arr.unshift(9));
/ / 7
console.log(arr);
// [9, 2, 3, 4, 5, 6, [7, 8]]
Copy the code

application

// Change the last item of the array to the first item
var arr1 = [2.3.4];
arr1.unshift(arr1.pop());
console.log(arr1);
// [4, 2, 3]

// Change the first item of the array to the last
var arr2 = [2.3.4];
arr2.push(arr2.shift());
console.log(arr2);
// [3, 4, 2]
Copy the code

5. Array merge and split

  • Array merging

    Concat () : Concatenates two or more arrays, returns a new array without changing the existing one

    var arr1 = [1.2.3];
    var arr2 = [4.5.6];
    var arrNew = arr1.concat(arr2);
    console.log(arrNew);
    // [1, 2, 3, 4, 5, 6]
    console.log(arr1);
    // [1, 2, 3]
    
    var arrNew2 = arr1.concat(4.5[6.7]);
    console.log(arrNew2);
    // [1, 2, 3, 4, 5, 6, 7]
    Copy the code

    Note: Arrays are generated by adding all the arrayX parameters to the arrayObject. If the argument to concat() is an array, then you add elements of the array, not the array.

  • Array splitting

    Slice () : Returns selected elements from an existing array.

    Grammar: arrayObject. Slice (start, end)

    Return value: Returns a new array containing elements from the arrayObject from start to end (excluding the element).

    Start: Where the selection must start, if negative, from the end of the array (-1 is the last element)

    End: The element selected (not included) from where it is optional, from start to the end of the array if it does not exist, or from the end of the array if it is negative

    var arr = [1.2.3.4.5.6.7.8];
    
    var arrNew = arr.slice(2.5);
    // Get a new array
    console.log(arrNew);
    // [3, 4, 5]
    // The length of the new array is end-start
    console.log(arrNew.length);
    / / 3
    
    var arrNew1 = arr.slice(-6, -2);
    console.log(arrNew1);
    // [3, 4, 5, 6]
    
    // Only start is positive
    var arrNew2 = arr.slice(3);
    console.log(arrNew2);
    // [4, 5, 6, 7, 8]
    
    // Only start is negative
    var arrNew3 = arr.slice(-6);
    console.log(arrNew3);
    // [3, 4, 5, 6, 7, 8]
    Copy the code

    Note: This method does not modify the array, but returns a subarray. If you want to delete an element from an Array, use the array.splice () method.

6. Delete, insert, and replace arrays

Splice () : Adds/removes items to/from the array and returns the deleted items.

Note: This method alters the original array

Grammar: arrayObject. Splice (index, howmany, item1,… ,itemX)

Index: A required integer specifies where items are added/removed, and a negative number specifies the location from the end of the array

Howmany: Number of items that must be deleted If set to 0, items will not be deleted

item1, ... ItemX: Optional new item to add to the array

Return value: Array of deleted elements, if not, empty array, source array changed

var arr = [1.2.3.4.5.6.7.8];
/ / delete
var res1 = arr.splice(2.3);
console.log(res1);
// [3, 4, 5]
console.log(arr);
// [1, 2, 6, 7, 8]

/ / insert
var res2 = arr.splice(2.0.23.34.'hello');
console.log(res2);
/ / []
console.log(arr);
// [1, 2, 23, 34, 'Hello ', 6, 7, 8]

/ / replace
var res3 = arr.splice(2.3.100.101);
console.log(res3);
// [23, 34, 'Hello']
console.log(arr);
// [1, 2, 100, 101, 6, 7, 8]
Copy the code

7. Array reverse ordering and sorting

  • Reverse order

    Reverse () : Reverses the order of array elements

    var arr = [1.2.3.4.5.6.7.8];
    / / reverse
    console.log(arr.reverse());
    // [8, 7, 6, 5, 4, 3, 2, 1]
    Copy the code
  • The sorting

    Sort () : Sort the elements of an array.

    Grammar: arrayObject. Sort (sortby)

    Parameter: sortby: Optional specifies that the sort order must be a function; If there is no argument, convert the element to A string and sort it from smallest to largest in the character encoding order (numbers 0-9< uppercase a-z < lowercase A-z)

    / / no parameters
    var arr = [12.2.'AB'.4.5.null.'x'.'a'];
    / / reverse
    console.log(arr.sort());
    // [ 12, 2, 4, 5, 'AB', 'a', null, 'x' ]
    Copy the code

    Parameter (function) : This function compares two values and returns a number indicating the relative order of the two values. The comparison function should take two arguments, a and b, and return the following values:

    • ascending
      • If a > b, return a value greater than 0.
      • If a = b, 0 is returned.
      • If a < b, return a value less than 0.
    • The descending order
      • If a > b, return a value less than 0.
      • If a = b, 0 is returned.
      • If a < b, return a value greater than 0.
    var arr = [12.34.56.4.5.100.3.35];
    console.log(arr.sort(compareFun1));
    var arr = [12.34.56.4.5.100.3.35];
    console.log(arr.sort(compareFun2));
    
    // Ascending function
    function compareFun1(a, b) {
      if (a > b ) {
        return 1;
      } else if (a === b) {
        return 0;
      } else {
        return -1}}// descending function
    function compareFun2(a, b) {
      if (a > b ) {
        return -1;
      } else if (a === b) {
        return 0;
      } else {
        return 1}}Copy the code

8. Convert arrays to strings

Join (separator) : Used to put all the elements of an array into a string

Parameter: Optional specifies the delimiter to use if this parameter is omitted, use a comma as the delimiter.

Return value: Returns a string. The string is generated by converting each element of the array to a string, then concatenating the strings and inserting the separator string between the two elements.

var arr = ['001'.'002'.'003'.'006'.'017'];
console.log(arr.join());
/ / 001002003006017
console.log(arr.join('. '));
/ / 001.002.003.006.017
Copy the code