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

Editing Array Data

How to change data in an array. That's what we are looking into in this video.

Video Transcript

00:00In our last example,
00:01we've ended by just approaching our data,
00:04and I've created a quick little two new arrays
00:07similar to the way we worked before.
00:09So our first array is a list of our users,
00:11while the second array is a list of permission levels.
00:15Because the site has only two different permission levels,
00:18I just set it as true or false
00:19using our Boolean values that we've met in earlier chapters.
00:23So with our Boolean values,
00:24let's start ahead and just create a little function here
00:27that would just retrieve that information.
00:29So I'm going to create a function
00:30which will be called message user.
00:33It would accept an ID, which would be a uint.
00:37It would not return anything.
00:40And because I'm working with CS5,
00:42it automatically closes the bracket for me.
00:45But you want to make sure if you're working
00:46in earlier versions,
00:47that your bracket that you open, you close.
00:51Okay, so now that we got our user ID,
00:53let's just trace out just our ID
00:56and just call that just for a second
00:58before we start even manipulating anything.
01:00So we're going to call message user.
01:04And we're going to send it an ID.
01:05And in this scenario, I just want to output my Ben message.
01:08So I'm going to send zero.
01:09Right now we should just get an output of ID,
01:11which would be zero, and we're getting that.
01:14Next up, what I want to do is, as we said,
01:16let's just output the two values,
01:18the data of the user and the level of the user.
01:21So the first thing we want to do
01:22is we want to call a users.
01:24And as we've done before,
01:25we're going to open a bracket, a square bracket.
01:28We're going to send in our ID.
01:30We're going to close that bracket.
01:31Then I'm going to put a comma right here.
01:33And we're going to ask for our user level as well.
01:36And exactly in the same way, we're going to send that ID.
01:39And let's run our application
01:41and just see that we're retrieving the data
01:43and we are receiving Ben and we're receiving the value true.
01:47Now that we know we're getting those messages,
01:49let's put here an if else statement
01:50based on the value of our a user's level.
01:53So if our user is a true user, which is in the higher level,
01:57we could just grab this data
01:59or just type it a user level ID.
02:02And I don't really put your any question in my if statement
02:04because my array already contains the answer, true or false.
02:08So I don't need to put here anything special,
02:10just directly the variable.
02:11And if the variable comes as true,
02:13it would output whatever's inside of that if statement.
02:16And if not, it wouldn't.
02:18So let's put here an else so we could output something else.
02:21So let's just change our trace to a low Ben
02:30and let's add here another message.
02:32So let's just have here.
02:35And because we know it's already that level,
02:37we don't have to type all that in.
02:39And we're just going to say, you have rights.
02:46And on the other hand,
02:47let's just copy the same thing
02:50and trace out to user that doesn't have their level,
02:52you don't have rights.
02:57Kind of basic, let's run this and just see if it works.
02:59And we see that I have your little error,
03:01let's see what that is.
03:02And we can see that I'm missing the plus sign right here.
03:05So as you see, it's telling syntax error,
03:07expecting right parameter before you have rights.
03:10So if I come here, I could see
03:11it's expecting a right parameter,
03:12which means if I wanted to extend it,
03:14I need to add a plus or a comma as we've said earlier.
03:18Let's run this one more time.
03:20And by the way, I fixed it to both lines
03:22because I copy pasted it.
03:24And you can see a low Ben you have rights.
03:25Let's try another user that doesn't have rights
03:27and Dan doesn't have rights.
03:29So I'm just going to replace the value zero to one
03:31because Dan is false.
03:33Let's just make sure that Dan continues to be false.
03:36Perfect, so right now we created a sample
03:38that's very similar to the one that we worked on before.
03:41Let's see how we could change the data of users.
03:44So if for example, we want to change directly Dan's value,
03:48we could just go directly to a user level.
03:51And we could change the value directly
03:53and just say to value number one, right?
03:55Because Dan is number one.
03:57We want to make value number one true.
04:00Thus, if we make it true, obviously if we run it
04:02because we're retracing the message before we change it,
04:04then we won't see any difference, right?
04:06We're still going to see that he's false.
04:09But if we then trace our message one more time,
04:14after we create our new function,
04:16let me just open that up again so we could see it.
04:19So if I change it and then call it again,
04:21we're going to see that our second trace line
04:23should then output you have rights, okay?
04:26So now we've changed Dan.
04:27Let's just wrap that into a function
04:29that enables us to change properties of a user.
04:33So let's create here a quick function
04:34and we're going to call it function, change user level,
04:41which would accept an ID as well, right?
04:45It would not return anything
04:46because it would directly change that value.
04:49So how would we change the value?
04:50Again, we could just grab what we've just done here
04:53and we're already seeing that we're missing something
04:55because we don't want to always set it to true.
04:57So we want to add another property here, right?
04:59Another attribute.
05:01And that attribute will be is right.
05:09Yeah, why not?
05:10Is right.
05:11And is right would be a Boolean.
05:15And let's see what we have to change here.
05:17So one, we want to change our one to be ID.
05:19We want to change is right to be the value
05:23that's coming in from our change user level.
05:25So we created now a quick function
05:27that changes the user level.
05:29The next thing that we want to do
05:30instead of changing the user level directly,
05:33let's just comment that out.
05:36We can now call our function and send it.
05:41We want to change user level one
05:43and want to change this value to be true.
05:45Now, basically this is the exact same thing
05:47as the line before
05:48and although we're adding a few more lines of code,
05:50we're making it easier for us to change many users.
05:54So now if we wanted to change it to user level one,
05:59we could change user level, let's say zero
06:02and want to change then to be false.
06:05We could have done that as well.
06:06Let me just, again, just make the screen bigger
06:09so we could see everything at the same time.
06:12So we could see now, so I'm changing user level one,
06:14I'm changing user level two
06:16and we could comment that out
06:17and let's just trace out user one.
06:19And you could see that I forgot to put here a semicolon.
06:22And as we've said before,
06:23Flash doesn't really care about it.
06:25It's just a convention that it's a nice one to keep.
06:28So Flash is not going to give you any compiling errors
06:30if you don't put the semicolon,
06:31but if you notice that you haven't, put it back in.
06:35And if we run our application now that we've set zero and one,
06:38we'll see that Ben has no rights anymore, but Dan does.
06:42Okay, so we've seen now how we could approach our data
06:45in our previous example, we've seen it here as well.
06:48And we've seen how we could change our data as well.
06:51Now, if we wanted to, we could have created a function
06:53that now changes the value of the name,
06:56for example, if we want to change the user name.
06:59But that's easy enough.
07:00And that's something that you guys could play around with
07:02because it's basically the same thing
07:04as what we've done here only with the A users.

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!