1024programmer Java JavaScript array functions unshift, shift, pop, push usage examples_javascript skills

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

How to declare an array

The declaration of arrays in s can be declared in several ways

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 to indicate the initialization length of the array

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

The code is as follows:

var tmp = new Array(‘3’);
alert(tmp); // 3

We can create arrays using shorthand mode, so that we can create an array with only one numeric element 3

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

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 inserts an element before the first element of the array

The code is as follows:

// Use unshift to insert an element before the first element of the array
//Return 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 one time, in order from the left

The code is as follows:

// Use unshift to insert an element before the first element of the array
//Return 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 the first element of the array and returns the value of the popped element

Small example:

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:

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

The code is as follows:

// Use push to add multiple elements to 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 is as follows:

// Use push to add multiple elements to 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

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

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.
PuThe sh function can actually be implemented in this way

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.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/629655

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索