How to declare an array
The declaration of arrays in
s can be declared in several ways
var tmp = []; // Abbreviation mode
var tmp = new Array(); // directly create a new one
var tmp = Array(); // Or new can also be used
When creating a new array, you can pass in a parameter indicating the initialization length of the array
// When new, a parameter is passed in to indicate the length of the initialized array
var tmp = new Array(3);
alert(tmp.length); // 3
But if you want to create an array with only one element 3, it cannot be achieved using the new method, because the system will treat the 3 you pass in as the length of the array, unless you use quotation marks to treat it as a string, such as
var tmp = new Array('3');
alert(tmp); // 3
We can create arrays using shorthand mode, which allows us to create an array with only one numeric element 3
var tmp = [3]
alert(typeof tmp[0]); // number
You can also initialize multiple elements, and the value of the element can be of any type
// Simple mode creates array
// The elements of the array can be of any data type
var tmp = [3,true,8.5,{'name':'lizhong'},['a','b']];
alert(tmp.length); // 5
1. unshift insert an element before the first element of the array
// Use unshift to insert an element before the first element of the array
// Return array length
var tmp = ['a','b'];
var len = tmp.unshift('c');
alert(len); // 3
alert(tmp); // c,a,b
You can also insert multiple elements at one time, in order from the left
// Use unshift to insert an element before the first element of the array
// Return array length
var tmp = ['a','b'];
var len = tmp.unshift('c','d');
alert(len); // 4
alert(tmp); // c,d,a,b
2. Shift pops the first element of the array and returns the value of the popped element
Small example:
// Use shift to pop out the first element of the array
// Return the popped element value
var tmp = ['a','b','c'];
var val = tmp.shift();
alert(val); // a
alert(tmp); // b,c
If it is an empty array:
// Use shift to pop out the first element of the array
// Return the popped element value
var tmp = [];
var val = tmp.shift();
alert(val); // undefined
alert(tmp); // empty
3. push to add elements at the end of the array
Contrary to unshift, push adds elements to the end of the array and returns the length of the array after adding the elements
// Use push to add multiple elements at the end of the array
// Return the latest length of the array
var tmp = ['a','b','c'];
var len = tmp.push('d');
alert(len); // 4
alert(tmp); // a,b,c,d
You can also add multiple elements at once
// Use push to add multiple elements at the end of the array
// Return the latest length of the array
var tmp = ['a','b','c'];
var len = tmp.push('d','e','f');
alert(len); // 6
alert(tmp); // a,b,c,d,e,f
4. The pop function deletes the element at the end of the array
Contrary to shift, pop pops the element at the end of the array and returns the value of the popped element
// Use pop to pop up the last element of the array
// Return the popped element value
var tmp = ['a','b','c'];
var val = tmp.pop();
alert(val); // c
alert(tmp); // a,b
If the array is empty, return undefined
// Use pop to pop up the last element of the array
// Return the popped element value
var tmp = [];
var val = tmp.pop();
alert(val); // undefined
alert(tmp); // empty
Using the above four functions, we can do some queue processing, and we will not write code for specific cases.
The push function can actually be implemented like this
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 function operations will modify the array itself.