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

For Each...In : Getting The Value

What if we don't care about the key can we run through the values skipping the need to ask for the keys... well there is a way its called for each.

Video Transcript

00:00We've seen how we could retrieve the key value using the for in
00:04type of
00:05Loop the next thing we want to see it is actually how do we retrieve the value directly?
00:11So I kept the array and the object that we had it earlier
00:14And let's look at the syntax of how we could retrieve the value directly without reaching the key first
00:22The syntax is very similar the only change in between instead of having a for and then opening our bracket and
00:28then creating our var and
00:31For earlier it was our key which was a string in
00:36For example a and then we traced out our key right in the scenario. We want to use a value
00:44Our value instead of using a for in we want to use a for each in and to do that
00:52We just will type after our for we will type each and
00:56That's all we really have to change and instead of getting our key. We should retrieve our value now if we run this it's not
01:03Really, correct that we're setting it as a string right because we have the values inside of here which are integers
01:08But they could be of any type, but if we would type it as an integer
01:12We're basically integer. We're basically locking it to be an integer type which is completely fine in our scenario
01:18So the reason why I was working as a string is because flash will automatically
01:22Automatically convert it only prime is is what if we had an object inside of our rate?
01:27Which is also in you know a possibility if we would do that and we run our application
01:32If we run it as an integer it would convert it into zero
01:37Which we really wouldn't want to do that if we run it as a string
01:41It would then convert it into a string representation of an object
01:48Now if we even didn't want that to happen
01:50We could have ran we could have run it as a star and as a star or nothing
01:55Which is more or less the same thing only?
01:58This will help if you're if you're running in flex you're running in a more come in a more strict type of mode of architecture
02:04Then defining this will make sure that you won't get a compiling error
02:10But let's go back to our basic where we're only using integers
02:13And then that's an arrow. It's completely legitimate to just set our value to be an int
02:19All right, so we've seen how to fetch the value from an array
02:22Let's look at we fetch that value again from our object and again in our object
02:27We could have had many different value types, so we could have said it as nothing as a star which could mean anything
02:33It's a wildcard, or we could set it as a string as we know right now. We have strings inside of our object and
02:39Then we don't really need to reference the key anymore
02:43We get the value directly and if we run it again, but this time we're running it with our object
02:47We should get our values directly from our for each loop
02:53Now it's used a lot for each is use a lot when working with
02:57XML and as we when we dig into courses related to XML we're going to see we're going to revisit for each and see how
03:04We implement for each using XML, but the structure is basically the same thing

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!