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

For...In - Getting The Key

Now that we understand the key-value relationship its time to see how can we use a for loop to loop through the keys of an object/array (and how to grab the value using the key).

Video Transcript

00:00So the first after we've seen the combination of the key value combinations
00:05Let's see how we could leverage our for loops to actually access key and values as you can see
00:11I've created in advance an array and an object and both of them have multiple keys and values
00:17Now with a special syntax we could actually cycle through all these elements inside of our objects. Let's see how we do that
00:24So I'm first going to start with
00:26four and
00:28Then I'm going to open a round bracket then I'm going to define a new
00:33I'm going to define a new
00:35Variable I'm going to call it item and it's going to be of a type of string
00:40Because our keys are strings from flashes perspective behind the scenes
00:44Although your array right here
00:46You know your keys are integers because there's zero one two three four and so on
00:51Behind the scenes flash actually converts them into strings. So we're going to have to set our our values to be strings
00:59Now behind the scenes the next step is actually we're going to have to tell flash what what keys actually
01:06You know, and that's a great way. Why don't we just call it keys because it's going to be much more descriptive, right?
01:11So for our key for every key inside of what in our a so that's basically our syntax
01:18We first create a new variable in this case in our recall the key and we're asking for all the keys inside of a
01:26Now if we want to test this out and see if it works
01:28I'm going to open and close a curly bracket and then trace out my key
01:34And if I run it you can see also in my previous output
01:37We're going to see that it's going to output all of our different keys
01:40now after it created our key we could at the same same motion we could after that then output our value by
01:48approaching our a with the new key that we just received and
01:51By doing that we could actually retrieve also the key and also the value
01:56Alright, and in this way because we knew that we're cycling through this key and we didn't really care about the order and that's an important
02:03thing to know
02:04When we don't care about the order in which it comes in this scenario came back perfectly from 0 through 4
02:09But that doesn't necessarily mean that it's always going to be that way
02:13so when we don't care about the order and we just want to retrieve all of our
02:17variables that we defined inside of our array or in next sample that we'll create let's just comment this out and
02:25It's going to be
02:26Basically the same thing and because it's going to be basically the same thing
02:30I could just copy this and we're just going to change one thing or two things
02:34We're going to change from the key we were going to ask for the key and oh instead of in our a and
02:39Instead of outputting our a key we're going to output our o key because it's from our object
02:44And if you run this one, we should get the same thing
02:47We should get our keys and values keys and values and everything looks like it's working perfectly
02:53Now as I was going back and I was saying earlier that it's an important thing to note
02:57Is that the order although it looks like the order that we've put you know in our array was the order the correct order?
03:03You could see that in our
03:05Object order it's not outputting in the order of our object right last name is coming first even though first name was the first input
03:13it and the reason is is because it's none. It's not really part of that structure of the
03:18For in for in doesn't really respect an order and if it's important to you to use a specific order
03:25Then you would probably want to work with the traditional for loop
03:29All right, so we've seen now how we could access
03:32Our keys and we've seen using that keys how we could access our
03:37values
03:39what would happen if we just didn't care about the keys and we just wanted the values such as
03:44Maybe we wanted to make a summary for all the values inside of a

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!