array knowledge point in front-end javascript

Tip: After the article is written, the catalog can be generated automatically, how to generate the help document to the right

Article Directory

Preface

Common methods for finding, adding, deleting, modifying arrays in javascript;

Tip: The following is the main body of this article. The following cases can be used as reference.

1. What is an array?

Array: An array is an array in which a set of variables is stored.

For example: var list = ['10',good','Hello', 520]

         1. The data in the array has some correlation.

         2. The array is ordered from within (the corresponding position is its subscript value).

         3. Any data type can be placed in the array.

         4. You can put a dictionary in the list. The dictionary object can be retrieved by its dictionary key index, for example: var a= [{hello: "}];

2. Declarations of arrays

1. Literal Quantity

        var arr = [12,4,6,12,4,5,7];     // Subscript of array: 0~Array length-1

2. Constructors

        var arr1 = new Array(1,2,3);        //Constructor declares an array

        var arr2 = new Array(3);         // Declares an empty array of length 3

3. Searching, adding, emptying and modifying arrays by themselves

1. Find   => Array Name [Subscript Value]

        var arr = [1,2,3,4,5]
        console.log(arr[1]);    //Find a member marked 1 below
		console.log(arr[arr.length-1]);    //Find the best member

2. Increase

        var arr = [1,2,3,4,5]
        arr[9] = 100;        //Add 100 to position with subscript value of 9
		console.log(arr);

3. Empty

        var arr = [1,2,3,4,5]
        arr.length = 0;        //Empty arr
		console.log(arr);

4. Modification

        var arr = [1,2,3,4,5]
        arr[2] = 16;        //Modify 3 in arr to 2
        console.log(arr);

4. Common methods of arrays:

1. push method: append members to the end of the array to return the length of the array

        var arr = [1,2,3,4,5]
        var len = arr.push(21,12,4,6,718);        //You can add one or more
		console.log(arr,len);        

2. unshift method: append members to the array header to return the length of the array

        var arr = [1,2,3,4,5]
        var len = arr.unshift(21,12,4,6,718);        //You can add one or more
		console.log(arr,len);

3. pop() method:   Delete end member of array   Return deleted members

        var arr = [1,2,3,4,5]
        var val = arr.pop();        //Delete the last member of the array
		console.log(arr,val);

4. shist() method:   Delete Array Header Member   Return deleted members

        var arr = [1,2,3,4,5]
        var val = arr.shift();        //Delete the first member of the array
		console.log(arr,val);

5. splice(start,num)     Method:       

(start): Delete from where the index is; (num): delete several consecutive backwards     

    Return to the new array Place deleted members in the new array

5.1, Delete

        var arr = [1,2,3,4,5]
        var val = arr.splice(1,2);        //(Index 1, 2 consecutive bits backward)
		console.log(arr,val);

5.2, Replacement

        var arr = [1,2,3,4,5]
        var val = arr.splice(1,1,66);        // (Index 1, 1, replacement value 66)
		console.log(arr,val);

5.3, Insert

        var arr = [1,2,3,4,5]
        var val = arr.splice(1,0,66);        // (index 1, 0, insert value 66)
		console.log(arr,val);

6. join() method: Returns the members of an array by concatenating them with the specified characters

        var arr = [1,2,3,4,5]
        var res = arr.join('+');        // "join" is the default value for why arrays are connected by what
		console.log(res,typeof res);

7. indexOf() method: detect the subscript position of the current member in the array

        var arr = [1,2,3,4,5]
        var index = arr.indexOf(4);
		console.log(index);        //Result 3    
		

        var index = arr.indexOf(100);
		console.log(index);        //The result is -1 100 in the arr array

Tags: Javascript Front-end ECMAScript array

Posted on Thu, 02 Dec 2021 12:19:52 -0500 by jrough