0

我对backbone.js 很陌生,我正在尝试显示嵌套数组值,但我显示的只是ojbect 对象。

我知道数据已返回,因为我可以在响应中看到它(见下文)。

<span class="lbl"> <%= title %> <%= status %> <%= questions[0] %></span>

以上显示Programming Draft[object Object]

我也试过

<span class="lbl"> <%= title %> <%= status %> <%= questions.question %> </span>

但它返回编程草案

回复

{
    "title": "Programming",
    "category": "IT",
    "_id": "5269344db00754f370000012",
    "__v": 0,
    "status": "Draft",
    "questions": [
      {
        "_id": "5269344db00754f37000000e",
        "difficulty_level": "Medium",
        "question_type": "Pick One",
        "question": "How long is a piece of string?",
        "tags": [
          "General"
        ],
        "answer_options": [
          {
            "a_option": "Way too long!",
            "answer": "False"
          },
          {
            "a_option": "Not long enough",
            "answer": "False"
          },
          {
            "a_option": "A Frayed Knot",
            "answer": "True"
          }
        ],
        "allow_dont_knows": false,
        "status": "Draft",
        "requires_approval": false
      },
      {
        "_id": "5269344db00754f37000000f",
        "difficulty_level": "Medium",
        "question_type": "Pick One",
        "question": "What is the nearest planet to earth?",
        "tags": [
          "Science",
          "Space"
        ],
        "answer_options": [
          {
            "a_option": "Mars",
            "answer": "False"
          },
          {
            "a_option": "Venus",
            "answer": "True"
          },
          {
            "a_option": "Jupiter",
            "answer": "False"
          },
          {
            "a_option": "Neptune",
            "answer": "False"
          },
          {
            "a_option": "Saturn",
            "answer": "False"
          }
        ],
        "allow_dont_knows": false,
        "status": "Draft",
        "requires_approval": false
      },
      {
        "_id": "5269344db00754f370000010",
        "difficulty_level": "Medium",
        "question_type": "Pick One",
        "question": "What is the largest planet?",
        "tags": [
          "Science",
          "Space"
        ],
        "answer_options": [
          {
            "a_option": "Mars",
            "answer": "False"
          },
          {
            "a_option": "Venus",
            "answer": "False"
          },
          {
            "a_option": "Jupiter",
            "answer": "True"
          },
          {
            "a_option": "Neptune",
            "answer": "False"
          },
          {
            "a_option": "Saturn",
            "answer": "False"
          }
        ],
        "allow_dont_knows": false,
        "status": "Draft",
        "requires_approval": false
      }
    ],
    "requires_approval": false
  }
]
4

1 回答 1

1

您需要访问数组中项目的索引,然后您需要指明要显示的属性的名称。

<span class="lbl"> <%= title %> <%= status %> <%= questions[0].question %></span>

如果要显示每个问题的问题,则需要添加对带有索引的问题的 for 和 inside 访问。

<% for(var i=0, len=questions.length; i<len; i++) { %>
<span class="lbl"> <%= title %> <%= status %> <%= questions[i].question %></span>
<% } %>
于 2013-10-25T12:40:16.920 回答