Common methods for arrays:

push(v1,v2,v3…) Pop () deletes a value at the end of the array Unshift () appends a value at the head of the array Shift () deletes a value at the head of the array Join () Turns the array into a string includes() Determines whether a value exists in the array indexOf() If there is a value in the array, return the first occurrence of the index, if not found, return -1 lastIndexOf(), return the last occurrence of the index concat() array, Return a new array slice(I, I) returns a new array (before but after) splice(I, number of deletions, new values…) Add, delete, replace – directly change the original array!!!!!!!

Usage:

Push, pop, unshift, shift as explained in my last blog post, here are some other ways to use them. Join (), includes(), indexOf(), lastIndexOf()

<script>
        var arr = [1.2.3.4.2.5];
        var str = arr.join('&');// Concatenate data with ampersand
        console.log(str);
        console.log(arr.includes(20));// There is no 20 in array, return false
        console.log(arr.indexOf(2));// If 2 is present in the array, return 1 for the first occurrence of 2
        console.log(arr.lastIndexOf(2));// If 2 is present in the array, return the last occurrence of 2
    </script>
Copy the code

Concat () concatenates an array, returning a new array

        var arr1 = [1.2.3.4];
        var arr2 = [6.7.8];
        var arr3 = arr1.concat(arr2);
        console.log(arr3);// result is [1,2,3,4,6,7,8]
Copy the code

Slice (I, I) intercepts an array value (before packet, not after packet)

      var arr4 = [1.2.3.4.5.6.7];
        var arr5 = arr4.slice(2.5);// Select values with subscripts 2 through 4, excluding values with subscripts 5
        console.log(arr5);// result is [3,4,5]
Copy the code

Splice (I, number of deleted, value added…) Add, delete, replace – directly change the original array, often used this method. \

  • Usage 1: Insert data arr.splice(I, 0, v1,v2, v3….)
var arr = [1.2.3.4.5];
        arr.splice(arr.length, 0.'a'.'b'.'c');// Add 'a ',' b','c' to arr.length (array length)
        console.log(arr);/ / [1, 2, 3, 4, 5, 'a', 'b', 'c']
Copy the code
  • Usage 2: Delete arr.splice(I, number of deleted)
/ / array [1, 2, 3, 4, 5, 'a', 'b', 'c']
 arr.splice(2.2);// Start with the subscript 2
        console.log(arr);/ / [1,2,5, 'a', 'b', 'c']
Copy the code
  • Usage 3: Replace arr.splice(I, number of deleted, value replaced…)
/ / [1,2,5, 'a', 'b', 'c']
   arr.splice(3.3.'A'.'B'.'C');A,b, and c will be deleted, and then add the uppercase ABC
        console.log(arr);/ / [1,2,5, 'A', 'B', 'C']
Copy the code

Array judgment method:

Again, data types; Basic data type: number, string, Boolean, null, undefined Basic data type: function Array object array.isarray ()

 var arr = [1.2.3];
        console.log(typeof arr); // object cannot be determined by the basic data type
        console.log(Array.isArray(arr));/ / true,
Copy the code

Common problems with arrays:

Array deduplicating:

Method 1: Get a new array: you can use the includes() method mentioned above and return a Boolean value

<script>
        var arr = [1.2.3.42.1.3.2.2.1.3.5];
        var newArr = [];
        // Iterate over the array
        for (var i in arr) {
            // Check whether each value in the original array exists in the new array
             if(! newArr.includes(arr[i])) { newArr.push(arr[i]) } }console.log(newArr);/ /,2,3,42,5 [1]
Copy the code

Method 2: Directly change the original array, compare the first value with all the following values, delete the duplicate value, pay attention to the array collapse, because each time you delete a value, the length of the array will change, and then the subscript will change.

var arr = [1.2.3.42.1.3.2.2.1.3.5];

        // The number of rounds to compare
        for (var i = 0; i < arr.length - 1; i++) {
            // Compare the ith number with all the following numbers
            for (var j = i + 1; j < arr.length; j++) {
                // Delete as long as the same
                if (arr[i] === arr[j]) {
                    // Each time an array is deleted, the array collapses!!!!!!!!!!
                    arr.splice(j, 1); j--; }}}console.log(arr);//[1,2,3,42,5
Copy the code

Count the number of occurrences of each value in the array:

Method 1: Delete the array first, get a new array with no duplicate values, then compare each value in the new array with the original array, if the same value occurs, add one

var arr = [1.2.3.42.1.3.2.2.1.3.5];
var arr2 = [] ;
        for(var i = 0 ; i < arr.length ; i++) {
            if(! arr2.includes(arr[i])) { arr2.push(arr[i]) } }console.log(arr2);/ /,2,3,42,5 [1]
        // Compare each value in the new array with the original array
        for(var i = 0 ; i < arr2.length ; i++) {
            // Define the counter
            var count = 0 ; 
            for(var j = 0 ; j < arr.length ; j++) {
                if(arr2[i] === arr[j]) {
                    count++
                }
            }
            console.log(arr2[i] + 'Show up' + count + 'time');
        }
Copy the code

The results are as follows:



Method two: Delete while statistics

The first value in the array is compared with the next valueSplice ()Method, notice that the array collapses until the last value is gone

var arr = [1.2.3.42.1.3.2.2.1.3.5];
for (var i = 0; i < arr.length; i++) {
            // Compare the first value with the following value
            // count each number in the array at least once
            var count = 1;
            for (var j = i + 1; j < arr.length; j++) {
                if (arr[i] === arr[j]) {// count = 1
                    count++;
                    arr.splice(j, 1);
                    j--;// Note array collapse}}console.log(arr[i] + 'Show up' + count + 'time');
        }
        console.log(arr);
Copy the code



Method three: sort and then count

If the first value in the array is equal to the next value, then the first value in the array is equal to the next value. If the first value is equal to the next value, then the first value in the array is equal to the next value

// To make it easier to define a sorted array, you can use bubble sort, select sort, or sort()
 var arr = [1.1.1.1.2.2.2.3.4.4];

        for (var i = 0; i < arr.length;) {
            var count = 1;
            for (var j = i + 1; j < arr.length; j++) {
                if (arr[i] === arr[j]) {
                    count++;
                } else {
                    // As long as it is not equal, there is no identical later
                    break; }}console.log(arr[i] + 'Show up' + count + 'time');
            i += count// I jumps directly to the count position, because the repetitions are all in one place
        }
Copy the code

Callback function:

A callback function is a function that takes an argument to another function. Note, in particular, the distinction between the real participating parameters. The next article will talk more about callbacks.