There are some interesting ways to get data from objects and arrays using different loop formats, so
there's one called for each, which allows us to loop through array contents.
So it's a method that executes the function once for each array element.
So you see, we've got an example of an array.
There's a for loop that takes the array length and then loops through each one.
There's also the for each loop and then the four L in array and also outputs that information.
So we've got quite a few options looping through the contents of arrays and the same thing for objects
we can loop through an object using the let property in object format.
So the four instatement iterates over all non simbel properties of an object.
So the challenge for this lesson is to output the contents of an array in the console, sautéing each
item and index value.
And then also the second part is output the contents of an object in the console showing each value
and property.
So go ahead, pause the video and I'll show you the solution coming up.
So first of all, we need to create an object.
So let's construct a really simple object of a value of one really, really simple object here.
And this will work for even more complex objects as well, of course.
So we refresh and we see we've got a nice, simple object that we can work with.
So the first thing that we want to do is loop through the contents of this object.
So looping through and we can get the property in object.
So C we get the keys.
So A, B, C, so we know from the object section that there's a number of ways that we can get that
object value.
So using the property, we can get it using the bracket notation and we can get the values as well.
So we get the property and the value.
So if we have an array, so let's also create a really simple array and this is a typical for loop.
So you'll see these as well used in order to get contents out of the array.
So looping through and while W is less than array and we know that arrays have a length value so we
can access that and then incrementing w sort of a way out of our loop.
So log and we can log out the value of W value that's corresponds with the index of W so see we've got
the value as well as the corresponding and I'm going to shorten this one as well.
So do it comma, separate it within the console so it's a little bit cleaner and better to understand.
So this is the second one here where we're looping through the array contents and we're getting the
essentially the index value with W as we loop through and we're getting the values that's corresponding
with the item in the array.
Now the other option series had quite a few different ways that we can get the content out so we can
also go element in array and then loop through that.
And we've got a value for element and we also have a value for the array because the element is going
to be the index value being returned back within the console like that.
So we get the content the same way that we can then of course utilise within our code.
And then lastly, there's also the for each method which will run through each one of the elements within
that array.
We can get back the index value if we want, and then we can also get back that entire array.
So there's three parameters there.
And we can only sometimes if we only want just the first one, we don't need the index value.
If depending on what we're using the code for, we can leave the other ones out.
So those ones are optional.
So outputting into the console, we're going to output the index as well as the element that's corresponds
with that index value.
So C it returns back the same thing as the for loop.
And you can see the contents are the same.
And if you're curious about what's within array, it's actually just going to constantly output that
full array.
So this is something that isn't I don't see very often, but it is an option.
If you do want to make use of that, then you can output the entire contents of the array each time
as well.