Array (Array)
A type used to store multiple data sets
Var names = [' zhang Fei ',' Liu Bei ',' Guan Yu ']; console.log(names);Copy the code
Access to data elements in the form of subscripts (subscripts start at 0) array names[subscripts], names[0];
Var names = [' zhang Fei ',' Liu Bei ',' Guan Yu ']; console.log(names[1]);Copy the code
Definition of an array
Create an array using []
It can be initialized directly and is recommended
Var arr1 = [10, 30]; console.log(arr1);Copy the code
Create an Array using Array
var arr2 = new Array();
console.log(arr2);
Copy the code
Array access
The length attribute represents the length of a number.
Access an array with an element, assign a value, and get the contents of the element. The array is of variable length.
Sample code:
<! DOCTYPE HTML >< HTML >< head> <meta charset="utf-8"> <title></title> <script> var arr1 = [10,20,30]; console.log("arr1.length: "+arr1.length); var arr3 = []; arr3[0] = 100; arr3[1] = 200 arr3[100] = 999; console.log("arr3.length: "+arr3.length); console.log("arr3[2]: "+arr3[2]); </script> </head> <body> </body> </html>Copy the code
Effect screenshot:
Array methods
Push adds elements to an array
It’s like pushing
Sample code:
<! DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> var arr = []; arr.push(10); arr.push(20); arr.push(30); arr.push('Keafmd'); console.log("arr[0]:"+arr[0]); console.log("arr[1]:"+arr[1]); console.log("arr[2]:"+arr[2]); console.log("arr[3]:"+arr[3]); </script> <title></title> </head> <body> </body> </html>Copy the code
Effect screenshot:
Pop takes the element from the end of the array and removes the element from the array
It’s like pushing the stack
Sample code:
<! DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> var arr = []; arr.push(10); arr.push(20); arr.push(30); arr.push('Keafmd'); console.log(arr.pop()); console.log(arr.pop()); console.log(arr.pop()); console.log(arr.pop()); console.log(arr) </script> <title></title> </head> <body> </body> </html>Copy the code
Effect screenshot:
Shift takes the element from the first index of the array and removes it from the array
Similar to the team
Sample code:
<! DOCTYPE HTML > < HTML > <head> <meta charset="utf-8"> <script> var arr4 = [10,20,30,40]; console.log(arr4.shift()) console.log(arr4) </script> <title></title> </head> <body> </body> </html>Copy the code
Effect screenshot:
Slice slice
Sample code:
<! DOCTYPE HTML > < HTML > <head> <meta charset="utf-8"> <script> var arr5 = [10,20,30,40]; Console. log(arr5) console.log("arr5.slice(1) : "+arr5.slice(1)) console.log("arr5.slice(1,2) : "+ arr5. Slice (1, 2)) < / script > < title > < / title > < / head > < body > < / body > < / HTML >Copy the code
Effect screenshot:
The join and the split
Sample code:
<! DOCTYPE HTML > < HTML > <head> <meta charset="utf-8"> <script> var arr6 = [10,20,30,40]; console.log("arr6.join(\"-\"): "+ (arr6.join('-'))) ; var arrStr = '10-20-30-40'; console.log(arrStr.split('-')) ; </script> <title></title> </head> <body> </body> </html>Copy the code
Effect screenshot:
Writing is not easy, read if it helps you, thank you for your support!
If you are a computer terminal, see the lower right corner of the “one button three links”, yes click it [ha ha]
Come on!
Work together!
Keafmd