How to declare an array
Declarations of arrays in s can be declared in several ways
Copy CodeThe code is as follows:
var tmp = []; Shorthand mode
var tmp = new Array (); Direct New One
var tmp = Array (); Or new can also
In the new array, you can pass in a parameter that represents the initialization length of the array.
Copy CodeThe code is as follows:
When new, a parameter is passed to indicate the initialization of the array length
var tmp = new Array (3);
alert (tmp.length); 3
But if you want to create an array with only one element 3, then using the new method is not possible, because the system will treat your incoming 3 as the length of the array, unless you use quotation marks as strings, such as
Copy CodeThe code is as follows:
var tmp = new Array (' 3 ');
Alert (TMP); 3
We can create arrays using shorthand mode so that we can create an array with only one number element 3
Copy CodeThe code is as follows:
var tmp = [3]
Alert (typeof tmp[0]); Number
You can also initialize multiple elements, and the value of an element can be any type
Copy CodeThe code is as follows:
Simple mode to create an array
The elements of an array can be any data type
var tmp = [3,true,8.5,{' name ': ' Lizhong '},[' a ', ' B '];
alert (tmp.length); 5
Unshift inserting an element before the first element of the array
Copy CodeThe code is as follows:
Inserting an element before the first element of the array using Unshift
Returns the array length
var tmp = [' A ', ' B '];
var len = tmp.unshift (' C ');
alert (len); 3
Alert (TMP); C,a,b
You can also insert more than one element at a time, sequentially from the left
Copy CodeThe code is as follows:
Inserting an element before the first element of the array using Unshift
Returns the array length
var tmp = [' A ', ' B '];
var len = tmp.unshift (' C ', ' d ');
alert (len); 4
Alert (TMP); C,d,a,b
Second, shift pops the first element of the array and returns the value of the element being popped.
Small instance:
Copy CodeThe code is as follows:
Use shift to pop up the first element of an array
Returns the value of the element being ejected
var tmp = [' A ', ' B ', ' C '];
var val = Tmp.shift ();
Alert (val); A
Alert (TMP); B,c
If it is an empty array:
Copy CodeThe code is as follows:
Use shift to pop up the first element of an array
Returns the value of the element being ejected
var tmp = [];
var val = Tmp.shift ();
Alert (val); Undefined
Alert (TMP); Empty
Push to add an element at the end of the array
As opposed to unshift, push adds an element at the end of the array, returning the array length after adding the element
Copy CodeThe code is as follows:
To add multiple elements to the end of an array using push
Returns the latest array length
var tmp = [' A ', ' B ', ' C '];
var len = Tmp.push (' d ');
alert (len); 4
Alert (TMP); A,b,c,d
You can also add more than one element at a time
Copy CodeThe code is as follows:
To add multiple elements to the end of an array using push
Returns the latest array length
var tmp = [' A ', ' B ', ' C '];
var len = Tmp.push (' d ', ' e ', ' f ');
alert (len); 6
Alert (TMP); A,b,c,d,e,f
The pop function deletes the element at the end of the array
Instead of shift, pop pops up the element at the end of the array, returning the value of the element being popped.
Copy CodeThe code is as follows:
Pop-up array end elements using pop
Returns the value of the element being ejected
var tmp = [' A ', ' B ', ' C '];
var val = Tmp.pop ();
Alert (val); C
Alert (TMP); A, b
Returns undefined if the array is empty
Copy CodeThe code is as follows:
Pop-up array end elements using pop
Returns the value of the element being ejected
var tmp = [];
var val = Tmp.pop ();
Alert (val); Undefined
Alert (TMP); Empty
Using the above four functions, we can do some queue processing, the specific case will not write code.
The push function can actually be implemented as well.
Copy CodeThe code is as follows:
var tmp = [' A ', ' B ', ' C '];
Tmp[tmp.length] = ' d ';
Alert (TMP); A,b,c,d
Note: The above four functions unshift, shift, pop, and push functions will be modified on the array itself.
JavaScript array functions unshift, shift, pop, push usages