JavaScript array functions unshift, shift, pop, push usages _javascript tips

Source: Internet
Author: User
Tags array length javascript array

How to declare an array

The declaration of an array in S can be declared in several ways

Copy Code code as follows:

var tmp = []; Shorthand mode
var tmp = new Array (); Direct New One
var tmp = Array (); Or new, you can.

In the new array, you can pass in a parameter that represents the initialization length of the array
Copy Code code as follows:

When new, pass in a parameter representing the initialization array length
var tmp = new Array (3);

alert (tmp.length); 3

But if you want to create an array of only one element 3, then using the new method is not possible because the system will use the 3 you pass in as the length of the array unless you use quotation marks as a string, as
Copy Code code as follows:

var tmp = new Array (' 3 ');
Alert (TMP); 3

We can create an array using the shorthand mode so that we can create an array with only one number element 3
Copy Code code 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 Code code as follows:

Create an array in minimalist mode
The elements of an array can be any one of the data types
var tmp = [3,true,8.5,{' name ': ' Lizhong '},[' a ', ' B ']];
alert (tmp.length); 5

Unshift inserts an element before the first element of the array

Copy Code code as follows:

Inserts an element before the first element of the array using Unshift
Returns the length of an array
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, sequentially from the left
Copy Code code as follows:

Inserts an element before the first element of the array using Unshift
Returns the length of an array
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, returning the element value that was ejected

Small example:

Copy Code code as follows:

Use shift to eject the first element of the 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 Code code as follows:

Use shift to eject the first element of the array
Returns the value of the element being ejected
var tmp = [];
var val = Tmp.shift ();
Alert (val); Undefined
Alert (TMP); Empty

Third, push add element at end of array

In contrast to unshift, push adds an element to the end of the array, returning the length of the array after the element is added

Copy Code code as follows:

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 Code code as follows:

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

Four, pop function delete Array end element

In contrast to shift, the pop pops up with the end element of the array, returning the element value that was ejected

Copy Code code as follows:

Use pop to eject array end element
Returns the value of the element being ejected
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 code as follows:

Use pop to eject array end element
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 done as well.
Copy Code code 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, push function actions are modified on the array itself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.