An introduction to the various methods of defining arrays in javascript and the common functions
- 2020-03-30 02:56:14
- OfStack
Definition of array:
Method 1.
var mycars=new Array()
mycars[0]="sharejs.com"
mycars[1]="Volvo"
mycars[2]="BMW"
Method 2.
Definition and initialization together:
var mycars=new Array("Saab","Volvo","BMW");
Or:
var mycars=["Saab","Volvo","BMW"];
Javascript two-dimensional array, with a one-dimensional array to simulate:
Method 1.
var arr = new Array(['a','b','c'],['d','e','f']);
Arr [0] returns the first one-dimensional array, and arr[0][0] returns the first element of the first one-dimensional array 'a', the same below.
Method 2.
arr=new Array();
for(i=0;i<100;i++) {
arr[i]=new Array(...);
}
Method 3.
var arr=new Array(
new Array(),
new Array(),
new Array()
);
Javascript arrays do not need to be set in length, they are expanded by themselves, and the array name.length returns the number of elements
Common javascript array functions:
ToString () : converts an array to a string
ToLocaleString () : converts an array to a string
Join () : converts an array to a symbolically concatenated string
Shift () : removes an element from the head of the array
Unshift () : inserts an element at the head of the array
Pop () : removes an element from the end of the array
Push () : adds an element to the end of the array
Concat () : adds elements to an array
Slice () : returns the part of the array
Reverse () : reverse sort the array
Sort () : sort an array
Splice () : inserts, removes, or replaces an array element