JavaScript basic summary - array summary

The arrays in JavaScript are summarized as follows:
1. Arrays are containers used to store a group of different types of data. Functions can be declared by using constructor methods and literal methods; arrays can be accessed by subscript methods; array length can be obtained by arr.length.
2. Array stack methods include:

unshift(): inserts a value in the array header, and the return value is the array length
 push(): insert a value at the end of the array, and the return value is the length of the array
 shift(): delete the first value in the array, and the return value is the deleted value
 pop(): delete the last value in the array, and the return value is the deleted value

3. Array to string:

join("Connector converted to character, comma separated by default")

4. Array sorting:

reverse()Reverse numerical order (for example: var arr=[1,2,3];
    document.write(arr.reverse());Output value: 3, 2, 1)
sort()Element comparison size (there are two cases of comparison size, example 1 is as follows:        
    var arr=[5,2,3];
    var arrs=arr.sort();
    document.write(arrs);The output value is 2, 3, 5;
    //Example 2 is as follows:
    var arr=[1,21,3,0];
    document.write(arr.sort());The output value is 0, 1, 21, 3;
    //The comparison size of example 2 is based on the first element. The correct ascending order should be 0, 1, 3, 21. The code is as follows:
    var arr=[1,21,3,0];
    document.write(arr.sort(function (x,y){
        return x-y;
    })); The input value is: 0, 1, 3, 21;
    //The descending order is:
    var arr=[1,21,3,0];
    document.write(arr.sort(function (x,y){
        return y-x;
    })); The output value is 21,3,1,0;
    )
 //So sort returns a new array after sorting the array. Sort can receive an anonymous function to sort the array in ascending and descending order

5. splice: it has the functions of deletion, replacement and insertion

For example:
    var arr=[1,2,3,5];
    //splice replace value, replace 3
    var insertArr=arr.splice(2,1,"I am the replacement value");//Return deleted value
    document.write(arr+"<br />");//The input result is [1,2, I am the replacement value, 5]
    //splice inserts a value after 3
    var replaceArr=arr.splice(3,0,"I am the insertion value");
    document.write(arr+"<br />");//[1,2,3, I'm the insertion value, 5]
    //splice delete value
    var delArr=arr.splice(2); //Return deleted value
    document.write(arr+"<br />"); //[1,2]

6. Arr.indexof (the required item to be searched, and the position index of the starting point of the optional search) is detected from the head of the array
Arr.lastindexof (the required item to be searched, the position index of the starting point of the optional search) is detected from the end of the array
Example:

    var arr=[1,2,3,5,2,1,3];
    //Array header begins to detect the position of element 2
    var firstArr=arr.indexOf(2)
    console.log("The subscript of the first element 2 in the array is"+firstArr); //Output value is 1
    //Array tail begins to detect the position of element 1
    var lastArr=arr.lastIndexOf(1);
    console.log("The subscript of the first element 1 in the array is"+lastArr); //Output value is 5

7. The common array interview questions are as follows:
1) array de duplication

<!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
        <body>
        <script type="text/javascript">
            var arr=[1,2,3,1],//Original array
                newArr=[], //New array
                flag;
            for(var i=0;i<arr.length;i++){//Loop original array
                flag=true;
                for(var j=0;j<newArr.length;j++){ //Loop new array
                    if(newArr[j]==arr[i]){ //If there is the same number in two arrays, flag is false
                        flag=false;
                    }
                };
                if(flag){ //If flag is true
                    newArr.push(arr[i]);//Insert arr[i] into the new array
                }
            }
            document.write(newArr)
        </script>
        </body>
    </html>
 2)Array summation
 <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
        <body>
        <script type="text/javascript">
            //1. Calculate the sum of all elements in the given array arr
            var arr=[1,2,3];
            function arryAnd(){
                var sum=0;//Initial value of sum
                for(var i=0;i<arr.length;i++){
                    sum+=arr[i];//sum=sum+0+arr[i] and so on
                }
                console.log(sum);
            }
            arryAnd();
        </script>
        </body>
    </html>
3)Merged array
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
    <script type="text/javascript">
        //Merge array arr1 and array arr2. Do not modify the array arr directly, the result will return a new array
        var arr1=[1,2,3],
            arr2=[4,5,6],
            arr;
        function merge(){
            return arr1.concat(arr2)
        }
        console.log(merge());
        console.log(arr)
    </script>
    </body>
</html>

4) delete a number in the array, take 0 as an example

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
    <script type="text/javascript">
        var arr=[0,2,3,0,5,0],//Old array
            newArr=[];
        for(var i=0;i<arr.length;i++){ //Loop new array
            if(arr[i]==0) continue;
            newArr.push(arr[i]);
        }
        console.log(newArr);
    </script>
    </body>
</html>
5)switch Acquisition of the week
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
    <script type="text/javascript">
        //How to get the week
        var week=new Date().getDay();
        console.log(week);//Return number,0-6
        switch(week){
            case 0:
            document.write("Sunday");
            break;
            case 1:
            document.write("Monday");
            break;
            case 2:
            document.write("Tuesday");
            break;
            case 3:
            document.write("Wednesday");
            break;
            case 4:
            document.write("Thursday");
            break;
            case 5:
            document.write("Friday");
            break;
            case 6:
            document.write("Saturday");
            break;
            //Or the last day is as follows:
//            default:
//            document.write("Saturday");
        }
    </script>
    </body>
</html>


Tags: Javascript

Posted on Mon, 02 Dec 2019 10:49:23 -0800 by r3drain