JavaScript simulation push

  • 2021-01-25 06:54:46
  • OfStack

Task: First I define a variable var arr = [0,1,2,3,4,5]; Now I want to emulate the push method and add something to the end of the 5 in this array, what do we do? I'll give you five minutes to think about it. Please don't look down before thinking about it.

Analysis: We want to add something to the end of the array, right? The key is how we find the last position of the element. First not to think about the last one, let's think about how we usually access to an array of content, whether that is arr [n], right, if we visit one greater than the array index, then undefined, is simply not the index is undefined, if we are going to the array index is 1 plus 1 so if undefined straight is the position we are looking for, although this way you can, but I have a better way is to use length properties, The length attribute gets the length of the array, so the length is the index of the array +1, which happens to be the index to which we want to add content.

Code:


var arr = [0,1,2,3,4,5];
arr.length  Gets the last position of the array 

Task: Now that we've got the last position of the array, we can add content to it, so how? To whom to add, how to add.

What are some ways to add content to an array? The length that we get is the position that we're going to add.

Synthesis:


var arr = [0,1,2,3,4,5];
function Push(value){
arr[arr.length] = value;
}
Push(6);
console.log(arr); //[0, 1, 2, 3, 4, 5, 6]

Task: Now you can add content to it, but you can only add one at a time. What do you do?

Analysis: I suddenly miss my old friend for, who can help us to finish some repetitive things, so let's ask him to help us.

Code:

Analysis: How should we write this for? How much is i less than? Is it just the number of bits we add? So how many bits do we have to add? We don't really know, but one of our old friends does. It is arguments, and it gets the parameters we pass in. It is a pseudo array, which means it can also get the length of the arguments we pass in using the length attribute like array 1.

Code:


for(var i=0;i<arguments.length;i++){}

Analysis: so now the problem is coming, we add what where to go, go back to our mission, we want to add 1 at the end of an array position we incoming content, wear come in to us, so how to get our incoming content, we as if said arguments access to its content.

Code:


var arr = [0,1,2,3,4,5];
function Push{
for(var i=0;i<arguments.length;i++){
arr[arr.length] = arguments[i];
}
}
Push(6,7,8);

To complete.

Extracurricular expansion: have a foundation of friends
ok. This is done, but the problem is that this code only works with the array arr. If we want to provide this for all arrays we need to add this method to the Array prototype.


var arr = [0,1,2,3,4,5];

Array.prototype.Push = function{
for(var i=0;i<arguments.length;i++){
this[this.length] = arguments[i];
}
}
arr.Push(1,2,3)

If you want to know more about ES68en, you can read the full list of ES70en points in ES69en. You don't have to memorize this article.


Related articles: