5.16 Exercise: Blog publishing Object
In this exercise you'll create a blog post object with several properties: title, body, author, views (the number of times the post has been read), comments, and isLive (a boolean that says whether the post is published). Each comment must itself be an object with author and body. Use the object literal syntax; the actual values don't matter, the goal is to get comfortable with the syntax.
Solution outline
title,bodyandauthorare strings.viewsis a number, for example10.commentsis an array, because a post can have several comments.- Each comment in the array is its own object with two string properties.
isLiveis a boolean,trueorfalse.
const post = {
title: "...",
body: "...",
author: "...",
views: 10,
comments: [
{ author: "a", body: "d" },
{ author: "a2", body: "d2" }
],
isLive: false
};
console.log(post);
Log the post on the console and you can see that comments is an array containing two objects, and clicking each one in the inspector reveals its author and body. That's it for this little exercise on blog publishing objects — see you in the next video.
Summary
This exercise teaches you how to create a blog post object using JavaScript object literal syntax with key properties including title, body, author, views, comments, and isLive. The comments property is particularly important as it stores an array of comment objects, where each comment has author and body properties to represent multiple reader comments on a post. By completing this exercise, you'll learn how to structure nested objects and arrays to model real-world data like a publishing system.
Key points
- Create a blog post object using object literal syntax with properties: title (string), body (string), author (string), views (number), comments (array), and isLive (boolean)
- The comments property must be an array because a blog post can have multiple comments
- Each comment in the comments array is an object with its own author and body properties, demonstrating nested object structures
- Understand the difference between primitive types (strings, numbers, booleans) and complex types (arrays and objects) when designing object properties
- Use console.log() to inspect and verify your object structure, including examining nested properties of comment objects within the comments array
- Practice initializing objects with realistic data values to reinforce familiarity with object literal syntax
FAQ
Why is the comments property an array instead of a single comment object?
Because a blog post typically receives multiple comments from different readers. Using an array allows you to store all comments in one property, making it easy to access and manage them collectively.
What properties should each comment object contain?
Each comment object should have at least author (identifying who left the comment) and body (the content of the comment). This mirrors the structure of the main post but focuses on individual reader feedback.
How do you access a specific comment's author from the post object?
You use array indexing combined with property access, like post.comments[0].author to get the author of the first comment, or post.comments[1].author for the second comment.