How to declare an array
There are several ways to declare an array in s
The code copy is as follows:
var tmp = []; // Abbreviation mode
var tmp = new Array(); // directly new one
var tmp = Array(); // Or new
When new array, you can pass a parameter to indicate the initialization length of the array
The code copy is as follows:
// When new, pass a parameter to indicate the initialization 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 the 3 you pass in as the length of the array unless you use quotes to treat it as a string, such as
The code copy is as follows:
var tmp = new Array('3');
alert(tmp); // 3
We can create an array using abbreviation pattern, so that we can create an array with only one number element 3
The code copy is as follows:
var tmp = [3]
alert(typeof tmp[0]); // number
Multiple elements can also be initialized, and the value of the element can be of any type.
The code copy is as follows:
// Create arrays in simple mode
// 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 element in front of the first element of the array
The code copy is as follows:
// Use unshift to insert an element before the first element of the array
// Return the 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 once, and arrange them from the left in sequence.
The code copy is as follows:
// Use unshift to insert an element before the first element of the array
// Return the array length
var tmp = ['a','b'];
var len = tmp.unshift('c','d');
alert(len); // 4
alert(tmp); // c,d,a,b
2. Shift pops up the first element of the array and returns the value of the element that was popped up.
Small example:
The code copy is as follows:
// Use shift to pop the first element of the array
// Return the value of the element that was popped up
var tmp = ['a','b','c'];
var val = tmp.shift();
alert(val); // a
alert(tmp); // b,c
If it is an empty array:
The code copy is as follows:
// Use shift to pop the first element of the array
// Return the value of the element that was popped up
var tmp = [];
var val = tmp.shift();
alert(val); // undefined
alert(tmp); // empty
3. Push adds elements at the end of the array
In contrast to unshift, push adds an element at the end of the array, returning the length of the array after adding the element.
The code copy is as follows:
// 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
The code copy is as follows:
// 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 end element of the array
In contrast to shift, pop pops up the element at the end of the array, returning the value of the element that is popped up.
The code copy is as follows:
// Use pop to pop the end element of the array
// Return the value of the element that was popped up
var tmp = ['a','b','c'];
var val = tmp.pop();
alert(val); // c
alert(tmp); // a,b
If the array is empty, return undefined
The code copy is as follows:
// Use pop to pop the end element of the array
// Return the value of the element that was popped up
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 won’t write code for specific cases.
In fact, push function can also be implemented in this way
The code copy 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 operations will be modified on the array itself.