如何计算此数组对象中的响应数量?

时间:2012-10-31 17:57:09

标签: javascript jquery

如何计算每个“回复”部分中的回复数量?

 var json = {
  "Answers": [
    {
      "Responses": [
        {
          "ResponseID": 5,
          "RespondingUser": null,
          "ResponseDate": "\/Date(1351700419747)\/"
        },
        {
          "ResponseID": 6,
          "RespondingUser": null,
          "ResponseDate": "\/Date(1351700437387)\/"
        },
        {
          "ResponseID": 7,
          "RespondingUser": null,
          "ResponseDate": "\/Date(1351700448550)\/"
        }
      ],
      "AnswerID": 3,
      "AnswerText": "No"
    },
    {
      "Responses": [
        {
          "ResponseID": 9,
          "RespondingUser": null,
          "ResponseDate": "\/Date(1351700419747)\/"
        },
        {
          "ResponseID": 6,
          "RespondingUser": null,
          "ResponseDate": "\/Date(1351700437387)\/"
        },
        {
          "ResponseID": 7,
          "RespondingUser": null,
          "ResponseDate": "\/Date(1351700448550)\/"
        }
      ],
      "AnswerID": 4,
      "AnswerText": "Probably not"
    },
    {
      "Responses": [
        {
          "ResponseID": 50,
          "RespondingUser": null,
          "ResponseDate": "\/Date(1351700419747)\/"
        },
        {
          "ResponseID": 6,
          "RespondingUser": null,
          "ResponseDate": "\/Date(1351700437387)\/"
        },
        {
          "ResponseID": 7,
          "RespondingUser": null,
          "ResponseDate": "\/Date(1351700448550)\/"
        }
      ],
      "AnswerID": 5,
      "AnswerText": "Definitely not"
    }
  ],
  "QuestionID": 2,
  "QuestionText": "Steve Lamaq?",
  "ClosingDate": "\/Date(1351958948793)\/",
  "AskingUser": null
};

任何建议都会令人惊叹!非常感谢

1 个答案:

答案 0 :(得分:1)

var i = json.Answers.length,
    total = 0;

while(i--){
 total += json.Answers[i].Responses.length;
}

<强> EXAMPLE