Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,360 --> 00:00:00,690
All right.
2
00:00:00,700 --> 00:00:03,060
So here's the answer to the challenge.
3
00:00:03,180 --> 00:00:09,480
Now if you had any trouble trying to figure out how to create an object in Javascript then you would've
4
00:00:09,570 --> 00:00:13,490
come across the syntax for an object that looks something like this.
5
00:00:13,530 --> 00:00:18,180
So you would start with the keyword var to show that you're creating a new variable, then you would give
6
00:00:18,180 --> 00:00:21,270
that variable a name whatever it is that you wish,
7
00:00:21,430 --> 00:00:25,780
then you have an equal sign and you open a set of curly braces.
8
00:00:25,860 --> 00:00:33,050
Now inside the curly braces you have your key value pairs and you can have as many pairs as you wish
9
00:00:33,390 --> 00:00:39,540
and you can even nest key value pairs as the value for a particular key.
10
00:00:39,540 --> 00:00:46,770
So in this case the key and values that are in red is actually bunched together as a value for the second
11
00:00:46,770 --> 00:00:47,360
key.
12
00:00:47,580 --> 00:00:55,320
So with this refresher on Javascript objects in mind let's head over to our app.js and let's delete
13
00:00:55,380 --> 00:01:02,970
this console that log. And instead of console logging our post body and post title we're going to store
14
00:01:02,970 --> 00:01:05,390
it inside a object.
15
00:01:05,400 --> 00:01:08,950
Let's use the same syntax as we saw previously in the slides.
16
00:01:09,120 --> 00:01:15,000
So that's create a var and we'll call it a post and we're going to use the equal sign and then we to open
17
00:01:15,000 --> 00:01:16,640
up a set of curly braces.
18
00:01:16,980 --> 00:01:22,090
And this is the start of any Javascript object. Inside the curly braces
19
00:01:22,110 --> 00:01:24,810
I'm going to put in my key value pairs.
20
00:01:24,810 --> 00:01:30,210
So the two keys that I want are the title and the content.
21
00:01:30,240 --> 00:01:36,570
Now all I have to do is just to add the values of each of those keys. So we know that the content is
22
00:01:36,570 --> 00:01:37,630
basically this
23
00:01:37,650 --> 00:01:38,370
right?
24
00:01:38,400 --> 00:01:39,750
It's request.
25
00:01:39,750 --> 00:01:43,700
body.postBody.
26
00:01:43,870 --> 00:01:51,800
And- So the title is structured very similarly request.body.postTitle and then we have a comma
27
00:01:51,830 --> 00:01:58,850
separating our key value pairs and we complete object by closing it off with a semicolon.
28
00:01:58,850 --> 00:02:06,470
So now we can delete our console.log and we now have our brand new object that's called post that contains
29
00:02:06,470 --> 00:02:09,400
two key value pairs separated by a comma,
30
00:02:09,590 --> 00:02:16,380
one is going to be the title and one is going to be the content. And these values are passed over
31
00:02:16,520 --> 00:02:19,820
when that post route is triggered.
32
00:02:20,030 --> 00:02:26,390
Now the final thing to consider is that we mentioned previously that you should try to avoid using vars
33
00:02:26,450 --> 00:02:27,940
if at all possible.
34
00:02:28,130 --> 00:02:35,120
And the best type of programming practice is to start with the most restrictive type of variable which
35
00:02:35,120 --> 00:02:36,490
is a const.
36
00:02:36,560 --> 00:02:43,370
And as you remember consts cannot be changed after they've been created. And we're not planning on
37
00:02:43,370 --> 00:02:49,280
changing it inside here after it's been declared. We're only interested in storing it and then we'll
38
00:02:49,280 --> 00:02:52,350
use it inside one of our pages.
39
00:02:52,790 --> 00:02:58,880
So a const is probably the best keyword to put over here. Now if you use the var or if you use the let,
40
00:02:59,030 --> 00:03:05,430
that's completely fine. But just take a refresher of what we talked about in the scope lesson and
41
00:03:05,590 --> 00:03:09,230
when we mentioned these three types of variables.
42
00:03:09,230 --> 00:03:11,720
So now we're ready to move on to the next challenge.
4299
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.