0

I have data which is an array of questions, and inside each question is an array of optional answers. What I'm looking to do is to get the index of the question and the answer for each answer.

So I have handlebars loop through the questions then loop through the answers of each one. Here is that:

  {{#questions}}
  <ul>
    <li><strong>{{question}}</strong><br>
    {{#each answers}}
      {{@index}}={{this}},
    {{/each}}</li>
  </ul>
  {{/questions}} 

Here is the data that get's put in:

    data = {
        "questions" : [
            {
                "question":"Favorite Color",
                "answers" : ["red","blue","green"]
            },
            {
                "question":"Favorite food",
                "answers" : ["pizza","pasta","cats"]
            }
        ]
    }

So if I want the index of the answer, I can write @index, but if its an array inside an array, it doesn't work. You can supposedly write ../ to get variables from the parent, but array does not like that.

{{../@index}}-{{@index}}={{this}}

This should give me questionIndex-answerIndex=answer.

But I get an error, something like this:

/home/ubuntu/workspace/views/survey/index.hbs: Parse error on line 16: ... {{../@index}} {{this}}

Error: /home/ubuntu/workspace/views/survey/index.hbs: Parse error on line 16: ... {{../@index}} {{this}}

Here is an example showing it not working: https://codepen.io/samkeddy/pen/oeMeeO (if you take out {{../@index}} it will work.

1 Answer 1

2

Try {{@../index}}-{{@index}}={{this}}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.