JavaScript array functions include unshift, shift, pop, and push instances, and unshiftpush

Source: Internet
Author: User
Tags javascript array

JavaScript array functions include unshift, shift, pop, and push instances, and unshiftpush

How to declare an array

There are several methods to declare arrays in s.
Copy codeThe Code is as follows:
Var tmp = []; // shorthand Mode
Var tmp = new Array (); // a new
Var tmp = Array (); // or new

You can input a parameter in the new array to indicate the initialization length of the array.
Copy codeThe Code is as follows:
// Input a parameter when the value is new to indicate the length of the initialized array.
Var tmp = new Array (3 );
 
Alert (tmp. length); // 3

However, if you want to create an array with only one element 3, the new method cannot be used, because the system regards the input 3 as the length of the array, unless you use quotation marks as a string, such
Copy codeThe Code is as follows:
Var tmp = new Array ('3 ');
Alert (tmp); // 3

You can create an array with only one number element 3 in the abbreviated mode.
Copy codeThe Code is as follows:
Var tmp = [3]
Alert (typeof tmp [0]); // number

You can also initialize multiple elements, and the element values can be of any type.
Copy codeThe Code is as follows:
// Create an array in simple mode
// The element of the array can be any data type
Var tmp = [3, true, 8.5, {'name': 'lizhong '}, ['A',' B '];
Alert (tmp. length); // 5

1. unshift inserts an element before the first element of the array

Copy codeThe Code is as follows:
// Use unshift to insert an element before the first element of the array
// Returns the length of the 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 a time, starting from the left
Copy codeThe Code is as follows:
// Use unshift to insert an element before the first element of the array
// Returns the length of the array.
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 element value to be popped up.

Small instance:
Copy codeThe Code is as follows:
// Use shift to pop up the first element of the array
// Return the pop-up element value
Var tmp = ['A', 'B', 'C'];
Var val = tmp. shift ();
Alert (val); //
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 the array
// Return the pop-up element value
Var tmp = [];
Var val = tmp. shift ();
Alert (val); // undefined
Alert (tmp); // null

3. add elements to the end of the array by push

In contrast to unshift, push adds an element to the end of the array and returns the length of the array after the element is added.
Copy codeThe Code is as follows:
// Use push to add multiple elements at the end of the array
// Returns 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 a time.
Copy codeThe Code is as follows:
// Use push to add multiple elements at the end of the array
// Returns 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

IV. The pop function deletes elements at the end of an array.

In contrast to shift, pop pops up the elements at the end of the array, and returns the element value to be popped up.
Copy codeThe Code is as follows:
// Use pop to pop up the elements at the end of the array
// Return the pop-up element value
Var tmp = ['A', 'B', 'C'];
Var val = tmp. pop ();
Alert (val); // c
Alert (tmp); // a, B

If the array is empty, undefined is returned.
Copy codeThe Code is as follows:
// Use pop to pop up the elements at the end of the array
// Return the pop-up element value
Var tmp = [];
Var val = tmp. pop ();
Alert (val); // undefined
Alert (tmp); // null
 
With the above four functions, we can do some queue processing, and the specific case will not write code.
The push function can also be implemented in this way.
Copy codeThe Code is as follows:
Var tmp = ['A', 'B', 'C'];
Tmp [tmp. length] = 'D ';
Alert (tmp); // a, B, c, d

Note: The unshift, shift, pop, and push operations of the above four functions are all modified on the array itself.


Can javascript push a variable to an array?

Can javascript push a variable to an array?
No problem. This is irrelevant to whether x is a variable, because only the actual content of x is used.

Urgent. Question about javascript array?

Var a = new Array (1) is used to create an Array with a length of 1.

Array is an Array built in javascript. That is, the system predefines an array class, as follows:

Class Array {
// This class has three constructor Methods
Array (){...}
Array ([size]) {...}
Array ([element0 [, element1 [, [, elementN]) {...}
// (I don't know how to implement the constructor)
// This class seems to have 13 Methods: concat, join, pop, push, splice, shift, unshift, etc. (If you forget it, check the related javascirpt tool)
Function concat (arr1, arr2 [, arr3 [, arr4 [...]) {}
}

Therefore, when var a = new Array (1) is executed, the system will call the Array ([size]) constructor to construct an Array object, and define a variable a pointing to this array object.

Because a points to this array object, it is customary to call a an array. Array (1), like Array (), is used to construct an Array instance object. Therefore, Array (1) and Array () are generally not called arrays, but arrays constructors.

You don't know what to say. I don't know if you asked this question.

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.