Objects away, it's time to array
Say "array away" 10 times fast. Not that easy. Lucky for us Arrays are not that hard, as we've been working with objects for quite a while now. Arrays are in many ways are a subset of objects. While objects are indexed by keys that are unique and not linear, arrays are numerically indexed starting from zero, progressing as we add more values.
Video Transcript
00:00incredibly short, short, short, short, short, intro.
00:05That's it, we're gonna start talking about arrays.
00:08I know you've been through a lot,
00:10but we have to learn a few more things,
00:13but we're almost done with learning new things
00:15for this chapter, and after that we're gonna practice
00:18and really, really practice all the stuff we've learned.
00:21So let's jump right in.
00:22Our next thing that we wanna learn about is arrays.
00:26Now, arrays are very similar to properties.
00:29The only difference between arrays and properties,
00:32remember when I called objects
00:34and I called them associative arrays?
00:37The reason is because there is an association
00:39between a key and a value.
00:42The biggest difference between an array
00:44and an associative array is arrays are organized
00:47by based on an index, and because of that,
00:49you don't need the key anymore, you just need the value.
00:52Now, the way you create an array is very simple.
00:55So I'm just gonna create a new array,
00:57and by the way, I could say new array,
01:00and that's basically the naming convention,
01:03and then I can send in whatever values I want.
01:05Let's say 10, 20, 30, let's say these are the values
01:08and I just comma-separate them,
01:10and I could put as many of them as I want.
01:12Once I create an array,
01:14I could basically access that information
01:16by basically just outputting it out.
01:18So I'm just gonna go ahead and show you.
01:19For example, I wanna see what's in position zero
01:22because it's positioned from index zero and onwards,
01:24so zero, one, two, and so on and so forth.
01:28So if I console log it out,
01:31I'll then be able to see the value 10
01:33because the item in position zero is the value 10.
01:37So if I go back into my sample here
01:39and I just click on refresh,
01:41you're gonna see that we're gonna get that value
01:43right ahead inside of our output.
01:45And if we wanted to, we could have very easily
01:48go ahead and get position one or two,
01:51and position one would be the value of 20.
01:53So if I go back there, click on refresh,
01:56I will get the value of 20.
01:58Now, that's literally all you really need to know
02:00about the basics of arrays,
02:02and we'll meet and learn a lot more about them later on.
02:05Not only could you create them
02:06and assign the values automatically,
02:08I could also then, if I want to,
02:10just go ahead and just start positioning values
02:12inside saying A zero, you're equal 100,
02:15and A one, you're equal something else, let's say 300.
02:20Now, I don't have to fill in a linear way,
02:24zero through whatever, but that is the preferred way.
02:43And we're gonna see why in the next chapter, not in this one.
02:48So if I save this, we'll see that if I assign the value
02:51into one of 300, we'll see that in position one,
02:55the value will be 300 now
02:56if we click on the refresh button.
02:58Okay, so that's basically,
02:59it's very, very similar to objects.
03:01No big difference, the only big difference
03:03is that instead of using strings as our associative array,
03:06we're putting here basically an indexed array
03:09starting from zero.
03:10So that's basically why it's called an array.
03:13All right, so I'm gonna comment this out
03:14because what I want to do
03:16is I want to basically duplicate our function
03:19that we've created of expand three, right.
03:22And we're gonna still call it expand three, right.
03:24But what I'm gonna do, I'm just gonna go ahead
03:26and just copy this whole thing
03:27and I'm gonna give it a new name.
03:29So I'm gonna go ahead, I'm just gonna copy it
03:30because I don't want us to destroy what we've created
03:33in case you wanna refer back to it.
03:35So literally, I just copied that function
03:37and I'm just gonna call expand right array.
03:44All right, so basically,
03:45we're gonna use an array for our property
03:48instead of using an object.
03:50And I'm gonna go ahead
03:51and I'm gonna show you how do I send an array in
03:53and then we'll go in and update our information.
03:56So first of all, I'm gonna call my A prop for our properties
04:00and I'm just gonna create a new array
04:02and I'm gonna show you a shorthand way
04:04of creating a new array.
04:05So we could set that new array just as we saw earlier,
04:08but a really common way of doing it
04:10is just putting a regular bracket, a squared bracket
04:15and that is how we create a new array.
04:17Now I could literally now go ahead
04:19and just send into that array all those properties.
04:22So I'm gonna go ahead here and send all the properties
04:24and just comma delimited.
04:27And literally, we're putting it in a specific order.
04:29For us, the order is important
04:31because we're ordering it from left to right.
04:36And there we go, we just created that array.
04:39All that's left for us to do
04:40is to literally call our array function here
04:43and I'm just gonna copy the same stuff
04:44that we had here earlier
04:46and just change it to expand right array.
04:50And I'm gonna go ahead and send as our property
04:54our A props array that we've just created.
04:57We're not gonna change anything else.
04:59What we wanna do is we're gonna go through this function now
05:02and now change the way we're talking to our properties
05:06as our props and we'll just call it,
05:08let me go ahead and call it A props
05:10just so it'll be very clear that we have your an array.
05:14And we're gonna go through any place
05:15that has one of our properties such as this one
05:18and then update it.
05:19So first of all, the first place we see it
05:21is we see the first three lines here,
05:24all of them refer to our array as a property.
05:29Basically we're setting our property
05:31and putting in the exact same information inside.
05:34Now, something really, really cool
05:35is that TweenMax accepts a few different types of properties.
05:38It could accept a string, it could accept an object
05:41but it could also accept an array.
05:43And when it accepts an array,
05:45it's gonna basically do everything
05:47that we put in a position to all the items in that array.
05:50So I'm gonna go ahead and delete this
05:52and instead of that,
05:53I'm gonna send in A props.
05:56I'm gonna go ahead and set that there.
05:57And for now, let me just return out of this function
06:02by commenting out everything from here on
06:04all the way until the end of the function.
06:07That way we could just focus
06:08on just one thing that we just did.
06:10So if I go right back in there into our browser
06:13and I click on refresh,
06:14we should see that all the items should be
06:16and we have very little error here.
06:18A prop is not defined.
06:20So let's make sure that we actually wrote it correctly
06:22because it's not a prop, it's A props, I believe.
06:26A props, all right.
06:27So let me just copy that and fix that small little error.
06:32Oopsie, and I'm just gonna save it
06:35and go right back in there and click on refresh.
06:38And you'll see now that all the three elements
06:40have disappeared.
06:41And just that alone, just what we did there alone,
06:45we just deleted three lines of code
06:46because we were using an array.
06:49So let's continue on comment,
06:51our commenting that we did before
06:53and let's continue down the line of seeing
06:56what and what and how, what and how do we change our elements.
07:00So just to make sure I'm gonna copy that A props,
07:02I don't have any confusions there.
07:04And I'm gonna look for the O props.
07:06So the first time I encounter it is right here
07:08where I'm trying to fetch the property itself.
07:10I'm gonna go ahead there and just
07:12instead of that approach the first item
07:14because it doesn't really matter
07:15which one we're approaching.
07:17Next is,
07:21oh, that's just a little,
07:23was that the first one really?
07:25Yeah, it was, all right.
07:25So let's continue.
07:26So this is all the same, there's nothing to change here.
07:29A props left, here we go.
07:31So we're gonna start for left is our first item.
07:33So I'm gonna A props zero instead of that.
07:37And center would be our second property.
07:43And this looks like center as well.
07:46So I'm gonna go ahead there and,
07:49oh, and I forgot this one as well.
07:51So this should be zero.
07:53And this should be one as well because it's the center.
07:57So that's our second property, which equals one.
08:00And our right property is the third one
08:02which is the second position.
08:04And there we go, we just changed all the values.
08:07All that's left for us to do is to test it out
08:09and see if it works.
08:10And if it does, then kind of cool.
08:13We use our first array and away array
08:16and seen it in action.
08:18So here we go.
08:19First of all, we have here a little problem.
08:21So PA prop is not defined
08:23which is in line 62.
08:24So I'm gonna go ahead and track that typo.
08:27I could see here that I left a element there.
08:29So I'm just gonna save that, remove that and refresh that.
08:33And here we go.
08:34Our animations are back to normal
08:36doing exactly what we expect from them to do.
08:39Now you might be thinking,
08:40well, that's really lovely that you could do that
08:41but what's so great about using an array
08:43instead of using properties?
08:46Now we still miss a few,
08:47a few, I would say critical elements
08:50to really complete this animation,
08:53but not really because what's going on here
08:56is we really would love to be able
08:59to make this completely dynamic
09:01where the properties could be 10 properties,
09:03five properties or three properties.
09:05For us to be able to do that,
09:07we probably need to learn something else
09:09but I'm gonna try with you in the next lesson
09:12to just improvise
09:13because with the skills that we learned so far
09:15and this is really part of the heart
09:16of becoming a great programmer
09:18is it's not necessarily knowing every single thing,
09:21it's knowing how to leverage what you know to the cap,
09:24to the max.
09:25So, and this is just a tip in general
09:27for you as a developer,
09:29don't try to read as much as possible
09:32but try to practice as much as possible.
09:34Knowing a lot, a lot of really great things
09:36doesn't make you a great developer
09:38but doing things in a lot of different ways does.
09:41Now, obviously I'm teaching you
09:43so I want you to learn
09:44but you don't have to be in a rush
09:46to learn every single thing
09:47that's not gonna automatically make you a better developer.
09:50Right, so in the next video, next lesson,
09:52what we're gonna do
09:53is we're gonna try to figure out a way to turn these lines
09:56that right now are relatively hard coded
09:58and try to make it a little bit more sophisticated
10:01so that we could basically re-leverage this
10:04and basically enable us
10:05to basically send in more than three
10:07without learning anything new
10:10but using the same skills that we learned so far.
10:13So let's do it right away.
10:14See you in the next lecture.
