Tail add (push)

The push() method adds one or more elements to the end of an array and returns the array's new length.

The push method does not change the index of the elements in the array as long as the elements to be added are placed at the end of the array. So loop through the argument list, placing the new elements at the end of the array.

Array.prototype._push = function(. value) { for (var i = 0; i < arguments.length; i++) {  
 this[this.length] = arguments[i]  
 } return this.length  
}  
var arr = [1.2.3.4]arr._push(9.8)  
console.log(arr) // [1, 2, 3, 4, 9, 8]
Copy the code

Add headers (unshift)

The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array (this method modifies the original array).

Adding elements to the head of an array changes the length of the array, but unlike adding elements to the tail of the array, the original element index does not change. To add headers, you need to move the index of the original element to the right. For example, if you add only one bit, you need to move the index of each element of the array one bit to the right. Assume that the array is 4 in length, and add an element to the header to change the length to 5.

So now it becomes: Array. length = 5, and currently **array[5-1]** is the last element, so array[5] must be the last element, so we can loop through the last digit of the array, Assign array[I] to array[i-1], stop at 1, and assign the 0th item of array to the value you want to add.

The process is as follows

Specific code implementation:

Array.prototype._unshift = function(value) { for (let i = this.length; i > 0; i--) {  
 this[i] = this[i - 1]}this[0] = value return this.length 
}  
var arr = [1.2.3.4]arr._unshift(8)  
  
console.log(arr); // [8, 1, 2, 3, 4]
Copy the code

But the above code only implements the header addition of one element, and the unshift method supports the addition of multiple elements. Such as:

var arr = [1.2.3.4]arr.unshift(8.7)  
console.log(arr); // [8, 7, 1, 2, 3, 4]
Copy the code

In this case, you need to know how many arguments are passed in. You can start with the arguments object, which loops back to the length of the array you last generated, moving the elements one by one, and then placing the new elements in the head of the array

New array is equal to the length of the + the number of parameters, the length of the original array from the back forward loop, will be the last of the original array, the last of the move to the new array, because of the need to insert the number of the head to the number of elements, so the length of the cycle of the starting point for the original array + the number of parameters, the end of the cycle for the number of arguments. But since the index is always one bit less than the length, both the start and end points need to be subtracted by 1.

Now we can write down the logic of the loop

Array.prototype._unshift = function(. value) { for (var i = (this.length + arguments.length - 1); i > arguments.length - 1; i--) { this[i] = this[i - arguments.length]  
 }}  
Copy the code

Think again, since the previous step has been moved, the position of the array header has been left empty, and the second step is to insert several elements for several parameters. So now we just need to loop in:

 for(var k = 0; k < arguments.length; k++) {  
 this[k] = arguments[k]  
 }
Copy the code

The complete code is as follows:

Array.prototype._unshift = function(. value) { for (var i = (this.length + arguments.length - 1); i > arguments.length - 1; i--) { this[i] = this\[i - arguments.length]  
 } for(var k = 0; k < arguments.length; k++) {  
 this[k] = arguments[k] } return this.length  
}  
var arr = [1.2.3.4\]arr._unshift(9.8)  
console.log(arr); // [9, 8, 1, 2, 3, 4]
Copy the code

Tail Deletion (POP)

The pop() method removes the last element of the arrayObject, reduces the array length by one, and returns the value of the element it removed. If the array is already empty, pop() does not change the array and returns undefined.

This is easy to implement, just follow the definition step by step. First, record the last element for return, then remove the last element from the array, point to NULL and release it, then reduce the length of the array by one, and finally determine whether the array is empty.

Array.prototype._pop = function () { if (!this.length) {  
 return undefined  
 } var end = this[this.length - 1] this[this.length - 1] = null this.length = this.length - 1 return end}  
  
var arr = [1.2.3.4]arr._pop()  
console.log(arr); // [1, 2, 3]
  
Copy the code

Header removal (Shift)

The shift() method removes the first element from an array and returns the value of the first element.

A header deletion changes the index of the original array element. That is, the index of the undeleted element is moved one bit to the left. First, the deleted element is recorded for return, then the first element of the array is pointed to NULL, and finally the array is looping through and the index is moved.

Array.prototype._shift = function () { if (!this.length) {  
 return undefined  
 } var start = this[0] this[0] = null for(var i = 0; i < this.length - 1; i++) { this[i] = this[i + 1]}this.length = this.length - 1 return start}  
  
var arr = [1.2.3.4]arr._shift()  
console.log(arr); // [2, 3, 4]
Copy the code