B
B
Bastard_Kiton2016-05-13 07:44:09
JSON
Bastard_Kiton, 2016-05-13 07:44:09

How to iterate json array in jade?

I'm building a simple static site, the assembler is gulp, the statics are in jade, there is data that is convenient to store in json.
Created the following construct in jade:

ol
  each question in questions
    li=question.q
      each val in question.answers
        label=val
          input(type="radio")
      
      span=question.current
      span К сожалению, неправильный ответ. Пожалуйста, ознакомьтесь с информацией по вопросу здесь: 
        a(href=question.checkLink, target="_blank")=question.checkLink

and this in the json file:
{
  "questions":{

    "question":{
      "q" : "some question 1",
      "answers" : ["Ответ первый", "Ответ второй", "Ответ третий", "Ответ четвертый"],
      "current" : 3,

      "checkLink" : "http://test.test/"
    },
    "question":{
      "q" : "some question 2",
      "answers" : ["Ответ первый", "Ответ второй", "Ответ третий", "Ответ четвертый"],
      "current" : 3,

      "checkLink" : "http://test.test/"
    },
    "question":{
      "q" : "some question 3",
      "answers" : ["Ответ первый", "Ответ второй", "Ответ третий", "Ответ четвертый"],
      "current" : 3,

      "checkLink" : "http://test.test/"
    }

  }
}

after building the page, I see in the statics I see only the last "question" - "some question 3", the two previous ones for some reason are not rendered. I suspect something is wrong with the json data. I would appreciate a detailed answer with a code example. Thank you. :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey delphinpro, 2016-05-13
@Bastard_Kiton

{
  "questions" : [
    {
       "q": "",
       ....
    },
    {
       "q": "",
       ....
    },
    ...
  ]
}

Are you writing code in notepad? Any IDE will tell you that your json code is invalid. Namely, the duplicate keys in the questions object , which should be an array at all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question