如何调用嵌套数据值

时间:2013-10-25 12:27:23

标签: javascript backbone.js

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

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

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

以上显示 编程草案[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
  }
]

1 个答案:

答案 0 :(得分:1)

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

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

如果您想要显示每个问题的问题,您需要使用索引添加对问题的内部和内部访问权限。

<% for(var i=0, len=questions.length; i<len; i++) { %>
<span class="lbl"> <%= title %> <%= status %> <%= questions[i].question %></span>
<% } %>