Home  >  Article  >  Web Front-end  >  JavaScript array functions unshift, shift, pop, push usage examples_javascript skills

JavaScript array functions unshift, shift, pop, push usage examples_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:38:352109browse

How to declare an array

The declaration of arrays in

s can be declared in several ways

Copy code The code is as follows:

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
Copy code The code is as follows:

// 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
Copy code The code is as follows:

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
Copy code The code is as follows:

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
Copy code The code is as follows:

// 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

Copy code The code is as follows:

// 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
Copy code The code is as follows:

// 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:

Copy code The code is as follows:

// 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:
Copy code The code is as follows:

// 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

Copy code The code 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
Copy code The code 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 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

Copy code The code is as follows:

// 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
Copy code The code is as follows:

// 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
Copy code The 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 function operations will modify the array itself.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn