preface
This article aims to consolidate the knowledge of people with weak js foundation, and can also be used to check deficiencies and fill gaps;
Tip: the following is the main content of this article. The following cases can be used for reference
1, First, what is an array?
A variable can store one value, but if you want to store multiple values, how should you write it?
Do you have to assign values to declarations one by one. As shown in the following code:
var str = "hello"; var str2 = "world"; var str3 = "happy";
If an array is used, it will become much simpler. There is no need for repeated operations. It can be completed in one line of code:
var str = ["hello","world","happy"];
2, Array creation:
/*Complete form*/ var Array name = new Array(Element 1, element 2, element 3); /*Simplified form*/ var Array name = [Element 1, element 2, element 3];
3, Get array:
How to get the elements after creating an array?
The elements in the array can be obtained by subscript. The subscript starts from 0: for example, if I want to obtain "word" in the array, the subscript is 1;
document.write() means that the result can be seen in the page line.
var str = ["hello","world","happy"]; document.write(str[1]);
4, Assignment of array:
If you want to reassign an array, you can use this:
var str = ["hello","world","happy"]; str[1] = "app"; document.write(str);
5, Detect the data type of the array, typeof (array name); The output result is Number;
var arr = 3; document.write(typeof(arr));
6, Get the length of the array, array name. Length; The input result is 5;
var str = [3,5,7,10,9]; document.write(str.length);
7, Loop through each element in the array: it is generally used in combination with length;
var str = [3,5,7,10,9]; for(i = 0; i < str.length; i++){ document.write(str[i]); }
8, Array usage:
1. Intercept a part of the array:
Array name. slice(start,end);
The interception range is [start, end], including strat but not end:
The output result is the part with subscript 3 to subscript 6, but excluding the element with subscript 6; [7,2,9]:
A parameter: indicates the part from subscript 3 to the end: [7,2,9,11]
var str = [3,4,6,7,2,9,11]; document.write(str.slice(3,6)); document.write(str.slice(3));
2. Add array elements:
Add elements at the beginning: array name. unshift();
Add elements at the end: array name. push();
3. Delete array elements:
Delete the first element of the array: array name. shift();
Delete the last element of the array: array name. pop();
4. Reverse array order (reverse order):
Array name. reverse();
5. Ascending order:
Sort by character encoding:
Array name. sort();
Sort:
var arr = [1,5,6,3,8,7,2,4,9,20,58]; //Add an argument to the comparison function arr.sort(function(a,b){ if(a > b){ return -1; //Indicates that a is ahead of b }else if(a < b){ return 1; //Indicates that a comes after b }else{ return 0; //Indicates that a and b remain unchanged } }) document.write(arr);
6. Connect two arrays:
Array name 1. Concat (array 2);
7. Delete, replace, and insert = splice
Delete all elements with subscript 2 and quantity 5:
Two parameters are required: array. splice(2,5);
The replacement subscript is 2 and the number is 4. The string to be replaced:
Three or more parameters are required: array. splice(2,4,"hello");
The insertion subscript is 2 and the quantity is 0. The string to be inserted:
Three or more parameters are required: array. splice(2,0,"hello","world","happy");
8. Find the first subscript of the element:
var str = [1,5,3,6,9,7,4,3,9,6]; document.write(str.indexof(3));
The output result is: 2
9. Find the index of the last occurrence of the element:
document.write(str.lastindexof(3));
The output result is: 7
10. To string (concatenating array elements into strings):
var arr = [1,5,6,3,5,8,9,10,7,4,2]; //Comma separated by default var str = arr.join(); console.log(str); //Use * to separate var str = arr.join("*"); console.log(str);
9, Empty array:
Method 1: arr = []
Mode 2: arr.length = 0;
Method 3: arr.splice(0,arr.length) ;
10, Check whether the instance object belongs to an object type:
var arr = [1,5,6,7]; document.write(arr instanceof Array);
Summary:
The above is my knowledge of the basic part of the array. If there is any omission, please leave a message to supplement it!
Source of note material: lagoon front-end training camp.