Stop Low Resolution Warning Sign

Transcript

Transcript of the tutorial. TOP


          
        
          So then now we know how to create objects using object literal notation like this.

          What I'd like to do is go back to this array right here, which is the blocks.
          
          And instead of storing strings in the array for each block, I'd like to store an object in each array
          
          for a block.
          
          So if you think about it, a block is more than just a title.
          
          It might have a title, maybe a number of likes, an author, etcetera.
          
          So it makes sense to store each block as an object instead of just a simple string.
          
          And we can do that in arrays.
          
          So just like we can store strings, numbers, booleans inside arrays, we can also store objects inside
          
          arrays.
          
          So first of all, let's do an example at the top.
          
          I'm going to create a constant called blogs.
          
          And I'm going to set this equal to an array.
          
          And inside this array I'm going to place two objects.
          
          So I'm going to enter down to a new line just to make this more readable.
          
          And the first element inside this array is going to be an object.
          
          And inside the object we're going to have a title property.
          
          The title for the first one is going to be Y, Mac and Cheese Rules.
          
          And then we're also going to have a likes property, which we'll set to 30.
          
          Now, we could have other properties such as content and author and stuff like that, but I'm going
          
          to keep this simple for now.
          
          So this object is the first element inside this array, much like this string is the first element inside
          
          this array.
          
          So remember, elements are comma separated inside arrays.
          
          So let's do a comma.
          
          Let's do our second element inside the array, which is also an object which represents another blog.
          
          And we'll have a title property for this.
          
          This title is going to be Ten Things to Make with Marmite, and we'll paste that in here.
          
          And then we're also going to have a likes property, which is going to be 50 right here.
          
          So now we have this blogs array.
          
          Let me just log this to the console.
          
          So console dot log and blogs.
          
          So if we save this, we can see now we have this array with two elements inside it.
          
          Each one you can see is an object.
          
          And when we expand this, the first one is this object and the second one is this object.
          
          So storing objects inside arrays is absolutely fine.
          
          And a lot of the time in the future when we start to work with data, this is the format that we'll
          
          be using because when we retrieve data from a database, it's going to be in some form of object format.
          
          In an array, for example, we might have a movie website and we might have a collection of movies.
          
          Now when we retrieve that collection of movies, we're going to have it in a format which is like an
          
          array of objects where each object would be a single movie, and then we could cycle through those and
          
          do something with each individual movie.
          
          So regardless of the data, this is going to be a very common format, an array of objects.
          
          So now we've seen how to do that.
          
          Let's now copy these two things and paste them down in here.
          
          So I'm going to delete the strings, come on to a new line and paste these objects inside.
          
          So now we have this new array of objects, which is the blogs.
          
          I'm going to comment this stuff out and then I'm going to come down here and inside log blocks.
          
          Now we want to instead log the title and the likes.
          
          So we'll say blog dot title and also blog dot likes, because now each blog here is going to represent
          
          the object.
          
          Okay?
          
          As we cycle through so we can use the title and the likes on that blog object.
          
          So let's save it and see if this works and we can see an error.
          
          Blogs is not defined, so that's probably because we're still logging this to the console, which we
          
          are.
          
          So comment that out and then save again.
          
          And now we should see this so we can see this sentence first of all.
          
          And then the two different blogs with the likes as well.