JavaScript array functions unshift shift pop and push use instances

  • 2020-03-30 03:45:54
  • OfStack

How to declare an array

The declaration of an array in s can be made in several ways


var tmp = [];  //Shorthand mode
var tmp = new Array(); //Directly new a
var tmp = Array();  //Or new can be

You can pass in an argument to the new array to indicate the initialization length of the array

//When new, pass in 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 of 3, then using the new method is not possible, because the system will take the 3 you pass in as the length of the array, unless you use quotation marks as strings, for example

var tmp = new Array('3');
alert(tmp);  // 3

We can use the shorthand mode to create an array so that we can create an array with only one numeric element, 3

var tmp = [3]
alert(typeof tmp[0]);  // number

You can also start multiple elements, and the values of the elements can be of any type

//Simple mode creates an array
//The elements of an array can be of any data type
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


//Use unshift to insert
before the first element of the array //Returns the array length
var tmp = ['a','b'];
var len = tmp.unshift('c');
alert(len); // 3
alert(tmp); // c,a,b

You can also insert more than one element at a time, starting from the left

//Use unshift to insert
before the first element of the array //Returns the array length
var tmp = ['a','b'];
var len = tmp.unshift('c','d');
alert(len); // 4
alert(tmp); // c,d,a,b

Shift brings up the first element of the array and returns the value of the element that was brought up

Small instances:


//Use shift to pop up the first element of the array
//Returns the value of the ejected element
var tmp = ['a','b','c'];
var val = tmp.shift();
alert(val); // a
alert(tmp); // b,c

If it is an empty array:

//Use shift to pop up the first element of the array
//Returns the value of the ejected element
var tmp = [];
var val = tmp.shift();
alert(val); // undefined
alert(tmp); //An empty < br / >

Push adds an element to the end of the array

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


//Use push to add multiple elements
to 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 more than one element at a time

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

The pop function deletes the element at the end of the array

In contrast to shift, pop pops the end element of the array and returns the value of the element being popped


//Use pop to pop the end element of the array
//Returns the value of the ejected element
var tmp = ['a','b','c'];
var val = tmp.pop();
alert(val); // c
alert(tmp); // a,b

If the array is empty, return undefined

//Use pop to pop the end element of the array
//Returns the value of the ejected element
var tmp = [];
var val = tmp.pop();
alert(val); // undefined
alert(tmp); // empty

 
Using the above four functions, we can do some queue processing, specific cases do not write code.
Push can be done the same way

var tmp = ['a','b','c'];
tmp[tmp.length] = 'd';
alert(tmp); // a,b,c,d

Note: the four functions unshift, shift, pop, and push all change on the array itself.


Related articles: