1
.Contents
        -for(var i = 1; i < length; i++)
          .QuestionFrame
            =(i+1) + ') ' + ti.FAQ_QUESTION
          .AnswerFrame
            =ti.FAQ_DESCRIPTION

In this code, I am tying to make variables looking like t1.FAQ_QUESTION, t2.FAQ_QUESTION, t3.FAQ_QUESTION, and so on. I have no idea how to concatenate t and i to make a single variable.

chachacha
  • 11
  • 2

2 Answers2

0

As described in this answer to a question about using dynamic variable names in javascript, all variables declared in the global context (outside of a function context), can be accessed as properties of the Global object. And because they're accessible as properties, you can use concatenation within bracket notation.

It's important to point out that the examples given in the answer above are for javascript that runs in the browser. The Global object for javascript in browsers is the window object.

However, Pug runs in Node—not the browser. This answer to a different question about What is the node.js equivalent of window[“myvar”] = value? notes that the Global object in Node is global.

So you should be able to do something like this in Pug:

.Contents
  - for(var i = 1; i < length; i++)
    - let qa = global['t' + i]
    .QuestionFrame= qa.FAQ_QUESTION
    .AnswerFrame= qa.FAQ_DESCRIPTION
Sean
  • 6,873
  • 4
  • 21
  • 46
  • Thank you for the answer, but maybe due to the less information given, qa.FAQ_QUESTION, qa.FAQ_DESCRIPTION is still undefined. – chachacha Nov 17 '20 at 16:36
  • ``` app.get('/faq.jade',function(req,res){ var sql = 'SELECT FAQ_QUESTION, FAQ_DESCRIPTION from faq'; connection.query(sql, function(err, topics, fields){ if(err){ console.log(err); res.status(500).send('Internal Server Error(faq.jade)'); }else{ var faq_content = { length : topics.length } for(let i = 0; i < topics.length; i++){ faq_content['t'+(i+1)] = topics[i] } console.log(faq_content); res.render('faq.jade', faq_content); } }); }); ``` – chachacha Nov 17 '20 at 16:45
  • @chachacha please edit your question to include this code. it's hard to parse as a comment without formatting. – Sean Nov 17 '20 at 17:11
0
.Contents
        each topic in topics
          .QuestionFrame
            =topic.FAQ_ID + ') '
            =topic.FAQ_QUESTION
          .AnswerFrame
            =topic.FAQ_DESCRIPTION
          br

for each loop helped me to solve this problem!

chachacha
  • 11
  • 2