Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

const templateData = { id, text }; need explanation for this . why put id ?

const templateData = { id, text }; need explanation for this . why put id ?

Steven Parker
Steven Parker
231,141 Points

We'd need a bit more context to determine the purpose of this little snippet. Can you provide a link the the course page this is from, and/or more of the code?

3 Answers

Steven Parker
Steven Parker
231,141 Points

The brackets create what is known as an object literal, which is then assigned to the variable "templateData". This object has an "id" property and a "text" property to start with (more properties are added in the following code). The properties are to be used by the "render" method later to create the view.

Thank you very much.

Steven Parker
Steven Parker
231,141 Points

johnywick — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!

router.get("/:id", (req, res) => {
  const test = req.query.test; // req.query.side   //side =question
  const id = req.params.id; //req.params.id

  if (!test) {
    res.redirect(`/cards/${id}?test=question`);
  }
  const text = cards[id][test]; // cards[0].question
  const { hint } = cards[id]; // data.cards[0].hint

  const templateData = { id, text };
  console.log(templateData);
  if (test === "question") {
    // if req.query === 'quesiton'
    templateData.hint = hint;
    console.log(templateData.hint);
    templateData.sideToShow = "answer";
    templateData.sideToShowDisplay = "Answer";
  } else if (test === "answer") {
    templateData.sideToShow = "question";
    templateData.sideToShowDisplay = "Question";
  }
  res.render("card", templateData);
});

templateData = { id, text } i dont understand this why id in braket .

const templateData = { id, text }; how to read this kind of coding. what its name?