pop and shift
We know how to add things now we need to see how to remove items from an array.
Video Transcript
00:00So we've seen how we worked with push,
00:02and we've seen how we work with unshift.
00:04So far we've been adding items in char array.
00:07Let's see how could we remove items from our array.
00:11And both this push and the unshift have a kind of
00:15a complimentary or kind of reverse action to what they do.
00:19So the push has a partner, which is partner is pop.
00:23As push adds things to the end of the list,
00:25pop removes things from the end of the list.
00:28Thus, if I just create here a new variable,
00:30and let's just call this variable last user,
00:34which would be a string,
00:35and we're going to go to our a users,
00:37and then we're going to ask for it to pop.
00:40We're going to ask to pop out the last item from our array.
00:44Now, if we trace out our last user,
00:49our last user would be art.
00:51And that is true,
00:52because the last user we've put in was art.
00:54Now, if we try to trace out our a users again,
01:00we should see now that a users now
01:02won't have art in its list.
01:06Okay, perfecto.
01:09So now we know how to work with pop.
01:11Let's see how do we work with unshift.
01:13Unshift works very similarly.
01:15So what if we wanted to get rid of Ben?
01:18The way we would do that,
01:19I'm just going to use now a new variable.
01:21So I'm going to create a new variable.
01:23Let's just type F4 so we could
01:25see everything in full screen,
01:30which would be a string as well.
01:32And we're going to take our a users,
01:34and we're going to ask to shift,
01:36which is the opposite of unshift.
01:39So shifting removes things out,
01:42while unshifting puts them in.
01:44So if we unshift the user,
01:46we're basically going to take the first user
01:48out of the array.
01:50Thus, if we trace out our first user,
01:53and we trace out our a users,
01:57we should see now that our first trace
01:59is first going to,
02:00let's put a little separator right here,
02:02just so it'll be easier for us to see
02:05where one thing starts and when the next thing ends.
02:08If we run it one more time,
02:09let's see what happens.
02:11So the first item we see that art went out,
02:13and that's exactly what we saw earlier.
02:16And now we see when we remove Ben,
02:17now our array becomes even smaller,
02:20while shift took out the first item in the array,
02:23pop took the last item.
02:25And it's really that simple,
02:27working with pop and shift.
02:29So with that said,
02:30let's just implement this logic and create a function maybe,
02:34that we could reuse in our application here,
02:36this mini application.
02:38So let's create a new function.
02:40And if we wanted to,
02:41well, we want to make sure that when we remove something,
02:44we remove it both from the a users
02:45and from the users level, right?
02:47So let's create a real quick function.
02:50And we'll call this pop user, right?
02:56We don't need to give any parameters.
02:58And notice again,
02:59that it closed automatically the bracket for me.
03:03We don't need to put any parameters
03:04because pop would always be the last item.
03:06So we don't need to ask for any IDs.
03:08We don't need anything.
03:09We don't need to add it.
03:10We're not adding, we're just removing.
03:12Let's say we just want to remove
03:13and we don't want to add anything else after that.
03:15So let's just do that.
03:16So a users dot pop.
03:21And we want to make sure that we do that
03:23also to the user level, right?
03:25Because if we're popping out the user,
03:27we would want to pop out the user level as well,
03:29because they correlate to each other.
03:33And same thing would be true
03:34if you wanted to create our shift, right?
03:38So I'm just going to duplicate my pop
03:40and I'm just going to call it shift.
03:42And all we have to do is just change our pop to be shift
03:46and bingo, done.
03:48And now instead of all of these lines right here,
03:51which we could comment out,
03:54we could say, let's say we want to get rid of
03:55the first or last user.
03:57We could always just call our pop user.
04:05And if we want to get rid of the first user as well,
04:08on top of the last user,
04:10so this would get rid of art, right?
04:13And no memory of art would be left in our application.
04:16And we could also shift out our user,
04:19which would then remove Ben.
04:22And then Ben would not exist anymore in the application.
04:25Thus, if we try to trace out again,
04:27all of our users and we want to trace out
04:31all of our user levels,
04:35we should see now that there should be only three left
04:38in our application.
04:40And indeed, we only have Dan, Chris, and Daffy left.
04:43Okay, so this was a much easier, much quicker video.
04:46Because now that we know what push is,
04:48and we really get it, and we know what unshift is,
04:51now we could see the correlation.
04:52So whenever we want to push something,
04:54its opposite role would be popping.
04:56So we could push our pop.
04:58Pushing would add in things in,
05:00and pop would remove them.
05:01And when we want to put things in the beginning of the list,
05:03we could unshift them to push them to the beginning,
05:07or shift to remove them.
05:09Now the only exception between pop, shift,
05:12and their opposites is first of all,
05:15if we look at it one more time,
05:16and I'm just gonna just, for example, a user's pop,
05:22you'll see that pop doesn't accept any property.
05:27It returns an item.
05:30Okay, so basically what I'm trying to say on this one
05:34is that pop, in a contrary to push,
05:37push you could put in multiple parameters.
05:39What I'm trying to get here
05:40is if you want to get rid of one thing,
05:42you could use pop.
05:43But if you were trying to get rid of more than one thing,
05:45you would have to call pop a few times.
05:47But there's easier ways than doing that.
