Objects as associative arrays
Objects have a hidden feature within them. Not only are they a wonderful bucket for many things they can be used as a reference for elements. Associative arrays is a common name for objects because objects are dictionaries for values. We call objects associative arrays because the keys(the variable names common properties) are the way we fetch the data. Let's see it in action it will be more logical.
Video Transcript
00:01I kinda lied to you because we're not talking about arrays yet, we're going to talk about
00:06it really soon.
00:07But we're going to talk about something that's called associative arrays.
00:10Now before you get terrified and jump off of a windowsill, the good news is that you've
00:15already worked with them.
00:16Because objects are also known as associative arrays.
00:20And the idea really is because there is an association between a key and a value.
00:26Now we've worked with the moto, but let's jump right in, you'll see what I mean, don't
00:31worry.
00:32By the end of this video, you're going to be totally aware of associative arrays because
00:35you already worked with them.
00:37And then we'll move on into arrays.
00:39Before we find that magical creature that we called array, I'm going to step away from
00:44arrays to talk about associative arrays.
00:48Now we worked with associative arrays, the reason is because every object is an associative
00:52array.
00:53Now what does associative array actually mean?
00:56So I'm going to go ahead into my editor here, and notice how when we were sending properties
01:02into our tween max, we send an object in there.
01:05Now although the tween max could accept many properties, and we didn't talk about even
01:10a fraction of them, we don't have to send all of them.
01:13And that's really the nature of an associative array where we have an association and the
01:18association is basically a key and a value.
01:22If we go back up here for a second and look at that kind of construct, we say when we
01:27create an object, we could say hey, new object if we want, right?
01:32Or if we want, we could define that new object by saying var, var o, and then setting it
01:39with the opening and closing curly bracket, the exact same thing.
01:44We set out so that we could put here a key and a value, sending properties as a key value
01:49combination.
01:50Now beyond that, that's one way of approaching our object, but after we create this key value
01:56proposition, we said that we could approach and access that information by just typing
02:02o and then the name of the key, and that would give us access to that value.
02:07Now there's one more way of approaching that data is that if we set here o and we open
02:13a bracket, and we put inside here as a string the key itself.
02:18Now this is really the definition of an associative array, but this and this are the same thing.
02:23Now the power of using this is if you want to do combinations such as k plus y, and it
02:30could put here some sort of equation to basically create that property, or I could send that
02:36property as an external variable.
02:39But we're not going to, in this lesson beyond talking about this in this really group
02:44of lectures, we're not going to talk much more about associative arrays, but I do want
02:49to show you how can we leverage and use an associative array to create some level of
02:55dynamicness in our expand right.
02:58So I'm going to go ahead to my expand right, and I'm just going to name this expand three
03:04right.
03:05Now the reason I'm saying three right is because we're always supposedly going to send
03:08three properties in.
03:09Now I'm not going to go ahead through the whole process of making everything here dynamic,
03:14but what I am going to do is I want to go ahead into my object here, into my expand right,
03:19and as the first property, I'm going to send in an object.
03:22So I'm going to go ahead and create that object really quickly.
03:26So there's going to be properties, oh, prop, and my old prop is going to be an object,
03:31and I'm going to send you the property.
03:33So I'm going to say, let's say we'll call this left, and for our left value, we're
03:37going to put inside the actual value of the left element, which is our name block.
03:43So I'm going to go ahead there and set that as, if we find it again, here we go left,
03:48I'm going to set that as the left value, I'm going to put your center one, and for our
03:54center element, I'm going to go ahead and find our center element, which was our date
03:58block.
03:59So I'm going to go ahead there and add that as our date block.
04:04And last but not least, let's put here also our right block, and I'm going to go ahead
04:09and find its name, which was button analyzed, and I'm just going to save that.
04:15So now that I created an object, I'm just going to space things out here a bit so they'll
04:19be a little bit more visually pleasing.
04:22And so I'm sending through properties, and they're inside of my object, and I could
04:26literally take this old properties and just send it into my function as my property.
04:31Now the advantage of doing this is that now in the future, if I want to use different
04:35properties, I could send different properties, and I don't have to depend on setting those
04:40properties immediately, initially inside of our application.
04:44So let's go ahead and sort this out.
04:46So next step is I want to set this function to accept that parameter as the first
04:50parameter. So I'm going to call this props, and it's not we're not going to have a
04:55default because it's going to be a mandatory parameter.
04:58So that's it. So we have our first parameter prompts.
05:00I'm going to go ahead and just comment out everything here, and we'll step by step
05:04reintroduce it as we add the logic into it.
05:08So now that we have our properties and they're all sitting inside of an object, we
05:12could go ahead and deal with this fading out first.
05:16So first of all, if we can't use this anymore, we're going to have to use three
05:20properties, right, because we have to send your one, two, three.
05:23So to do that, I'm going to go ahead and first I'm going to come here and ask
05:27for my props dot left.
05:29That's going to be the first one.
05:31And if I just copy this, I'm going to copy this three times.
05:35I'm going to use also my center and I'm going to do the same thing for the right
05:41item. Now, if everything worked according to plan, if I go back into my element and
05:47I click on refresh, we should see that everything is invisible.
05:50But we have your small problems here that reference error expand right is not
05:55defined. That's because we change the name of our function.
05:57That is because, by the way, because we're going to create expand right again
06:01after we learn about arrays and see how to make this even even sweeter.
06:06So I'm going to save this. But right now, this expand three right means that we
06:09could only expand three things and we're expecting to get all those three
06:12parameters, left, center and right.
06:14All right. So now we save that.
06:16Let's go now that we save that, let's go ahead and test out and see if it's
06:19working and it's definitely working.
06:21So we got that to disappear.
06:23Next step, we want to go back into our element again.
06:26And in our element, we're going to go ahead here and we want to start
06:30animating. So first of all, this book with copy, we can't really animate
06:35yet. So we're not going to we're not going to talk about we're going to step
06:38into the properties.
06:40So our name block, which was the first property, we're actually talking
06:44about the property left.
06:45So I could go ahead here and just change this value to be the property left.
06:50I could change here the value to the property left again, literally just sub
06:55replacing the actual values with static values from our object
07:00anytime that they're being called.
07:02So I'm literally just swapping them.
07:04And last but not least is property right.
07:08And I could go ahead there and sort that out as well.
07:12And now that I replaced all those properties, really, that's all I had to do
07:15to update my elements.
07:17If I click here, I could save this, go back into refresh mode.
07:21And if I go back into my browser and I click on refresh, we're going to see
07:25that now those items are back and I could see that I have a small error again
07:29in line thirty nine.
07:30So I'm going to just jump to line thirty nine.
07:32Oh, that's because we have your that comment out that we're not using.
07:37So I deleted that and let's refresh.
07:39So now we don't have any errors.
07:40Our animations are running exactly as we expected them to run.
07:43So let's sort out also our paragraph down here.
07:46And if we go back into our editor, I'm going to have to add that as another
07:49property that book would slide and we could do one of two.
07:52We could or add that in as another property of that properties.
07:56Or we could add that as an extra as as like a pad as some sort of a padding area
08:02and literally just send in that book with slide.
08:05And we probably want to make that as the second property because our padding
08:09is is not mandatory.
08:12So, oh, you know what, let's let's make this also not mandatory.
08:15I love that. All right.
08:16So let's make this not mandatory.
08:17Let's start with not sending it and let's add here.
08:20Another property in this property will be bottom, bottom, bottom item.
08:27All right. Now, by default, I want to set it to some sort of a value that is invalid.
08:32So I'm going to go ahead and set its value by default to be false, which falls
08:36by the way, will be perfect, because if I set it by default to be false,
08:40this would not be called unless it's true, because we could put here a little
08:44if here, if bottom item, remember when we used our ifs before
08:51and only if bottom item, then this item would actually run.
08:55So literally, if it's false, this would never actually call
08:58and only if we actually send something, this would be called.
09:01And I could literally re leverage this from false and it could be false
09:05and it could be also whatever value we send.
09:21So initially, if I don't send anything and I run my application,
09:26we'll see that nothing is going to happen.
09:29Nothing changed.
09:30But if I go ahead and I call the function one more time,
09:33but this time I actually sent to it that property.
09:37And I go ahead and I send to it the actual property of our paragraph,
09:40which was book with slide and let's find it just here.
09:44Dot book with slide, save that, go back into my browser.
09:48We should now expect it to animate.
09:51Now, we still have a problem.
09:52And how about we solve that problem also, because I would love
09:55to make this dynamic as well.
09:56Now, if we look at it and we go back into our application again,
10:00we'll see that right now we're hard coding.
10:02How much is going to animate?
10:04Literally, we're saying we're animating it to basically go to the
10:08actual top margin of four pixels.
10:11No, sorry, we're going to top margin 35.
10:14Now, in reality, it could be a completely different padding
10:18that's needed.
10:19So what I want to do is I want to try to figure out a way
10:22maybe to leverage the padding that the user put in.
10:24Maybe we automatically add the padding of whatever value is
10:28here, or maybe we add also the height of the actual element.
10:32Now, to do that, I really need to find out first.
10:34What is the margin currently?
10:36We haven't really seen that so far, but we're going to be
10:39looking at how to figure that out using jQuery in the next
10:42lecture.
10:43So before we move on to working with arrays, we're going to
10:45take a small detour because I really want to make this
10:48dynamic as well, because this is the last element here that
10:51is hard-coded that really shouldn't be hard-coded.
10:54Well, one of the last properties, there's a few more properties
10:57such as the delay probably and maybe also this margin top
11:00that would make sense for us to maybe make that dynamic
11:04based on the size of the element.
11:06But so what we're going to do is we're going to solve the
11:08delay conundrum and we're going to solve the margin top
11:12conundrum in the next lecture.
