02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

push

looking deeper into adding data into an array after it was created.

Video Transcript

00:00As we said earlier, we've said that arrays are indexed.
00:04And because we said they're indexed, there's an importance to their indexing.
00:09And I wanted to reuse this example.
00:12I'm just going to delete our actual actions and leave the functions the way they were for a minute here.
00:18And let's look at the example for a scenario.
00:20We have a new user in the system.
00:22And we want to make sure that our new user is the last person or last item in the array.
00:28We want to add them into the array.
00:30Now that could happen dynamically, such as a new user just gets added into the system,
00:34which is a very, very possible and kind of rational scenario.
00:38So let's see how we could do that.
00:40And the way we would add a user to the end of the list is by calling the push.
00:45We want to push a user into the array.
00:47You would think that push is pushing in line and maybe becoming first,
00:51but actually pushing is the end of the line.
00:53And the reason is, is because it's on the top of the deck.
00:56Well, more accurately, the first one, if you look at it as a deck of cards,
00:59the first one is on the top.
01:01And as we add users, we're adding them to the bottom of the list.
01:04Thus, if we want to add something on the bottom, we want to push them underneath.
01:07So let's see how we could we push a user.
01:10So if we just want to go, we're just going to focus on our users.
01:13And before we even make any changes, I just want to show a little trace.
01:16And we've seen that before a users, we're just going to trace out our user as is.
01:22We're just going to trace after that a little break,
01:25just to make sure that we don't get confused between the two.
01:29So just tapping your little string, just a separator.
01:32And now let's edit our A users.
01:35So we're going to go to our A users, and then we're going to push, as we've said.
01:39So I'm going to select push.
01:41And as the name imply, push is telling me that I can add one or more properties.
01:45I don't have to add only one.
01:47Let's start with one, and then we'll see later on how do we add more.
01:50So I want to add now another member, and that new member would be Sarah.
01:55And I'm pushing Sarah into the system.
01:58Now, if I traced out again our A users, our A users should update
02:02and Sarah should be in the end of the list.
02:05So if I run the application one more time,
02:07I'm going to see that Sarah now is in the end of the list.
02:10And I didn't need to know what is the end of the list.
02:12Just by calling push, it would automatically figure out what the end is
02:16and start appending to the end another user.
02:19You can see the power here, instead of you approaching the array directly
02:22and telling it at what position you want to put it in a much easier way,
02:26you could just push Sarah into her position.
02:29Now, you might be asking yourself, but I might be pushing her in,
02:32but I want to know what position was she pushed in.
02:35The easiest way to check that out is actually the push actually returns a value.
02:39So if I would trace out my push, and I could do that without any problems,
02:48I could trace out my push to see what value is coming back
02:51and push the value in.
02:53And if I run my application one more time,
02:55we're going to see that Sarah was pushed into position 6.
02:580, 1, 2, 3, 4, 5.
03:01So she's the sixth item in the list,
03:04thus she's in position number 5.
03:06Basically, it's returning to us the length of our new array.
03:10Now, if I wanted to put more than just Sarah,
03:12and I'm just going to keep the trace as is,
03:14because I could put Sarah in and I could put another user.
03:17So let's say we want to put inside Cyrus.
03:20So I've put Sarah in, and now I'm going to put Cyrus in.
03:22Let's see what happens now.
03:24So now it tells us that the new array length is 7.
03:28It doesn't tell us explicitly what position is Sarah in,
03:31what position is Cyrus is in.
03:33But now we know that there are 7 positions,
03:36and because we added two items,
03:38we can deduce that those two items would be 6 and 5.
03:42Okay, so now we've seen how to work with push.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!