The js array pop prototype method will remove and return the last element in an array. This method works okay for what it is intended to be used for, however it might not always be the best choice. For example what if it is the first element that is to be removed and returned, and also what about injecting elements while one is at it? So this post will center on the use of the js array pop method, but also other ways of getting one or more elements from an array such as shift, splice, and just the use of the bracket syntax with an index number.
The basic idea of the js array pop prototype method is to call the pop method off of an instance of an array, the result is for the last element in the array to be removed and returned.
This might work okay for the most part as a way to do this sort of thing with arrays, but it is by no means the only way to go about doing so. There is also the shift method that will do more or less the same thing only removing the first or zero index element of the array. In addition there is the splice method that is far more robust then both the pop and shift methods as it can be used as a way to do this sort of things with any element in the array.
The js array shift method works more or less in the same way as the js array pop method, but with one very important difference which is that it is the first element that is removed and returned rather than the last.
3 - The array splice method for popping out one or more elements anywhere, and to inject new ones also
So there is the js array pop method that will pop out an return the last element in an array, and the js array shift method that will pop out the first. However what if you want to pop out a certain index value between the two end points of an array? For this there is the splice method which is not to be confused with the slice method that works a little differently and will not mutate the array in place.
The js array spice method can be used to remove one or more elements for any index location in a given array by passing the desired index value as the first argument when calling the js array splice method. So just like the js array pop method the same result can be achieved with splice by passing one less from the length of the array for the index value to pop out and return the last element in the array. In addition the same effect of the shift method can be done by using the value 0 for the index value argument.
However in addition to being able to preform the same results as pop and shift the splice method can be used for any index value in the array, and on top of that more than one element can be removed. On top of all of that one additional feature of the splice method is that new elements can be injecting into the array also. So it goes without saying that the splice method is all around far more robust when it comes to mutating an array by way of purging as well as going about injecting new elements into an array.
Now for a more advanced example that involves popping out elements from an array using a means of doing so. For this example I will be using the splice method over pop because it allows for me to pop out just elements that meat a certain condition. In this example I have an array of particle objects that move in a certain heading and have life lost over time, when a particle has a life value of zero or lower the particle needs to be purge out from the pool of particles to make room for new ones that will be spawned in. So this is a good example of using a method like splice that does something a little more advanced.
Fpor this example I will want a method that I can use to create a state object, and another methods that serves as my rotten for spawning in particles.
So here I have the purge method that will purge out objects in the parts array of the state object that have a life value of zero or below.
So I will now want a main update method that uses my spawn and purge methods along with a render method and a main app loop.
So this is a nice quick example of a method like js array splice doing its thing. However it might not be the best option for making this kind of project. When working out something like this I often might use a fixed pool and just make it so that particles enter an inactive state when a condition is reached rather than purging them out and spawning in a new one.
So the js array pop method can be used to purge out and return the last element in an array, but there are other options for doing so. Also it might not always be the best idea to keep purging out elements in the first place depending on the nature of the project. There are of course many other tools in the toolbox then it comes to doing the same things without mutating an array in place, and some times it might be better to keep reusing the same resources over and over again rather than purging them out and creating them again over and over again.