简体   繁体   中英

How do I count the amount of Responses in this array object?

how do I count the amount of responses in each 'Responses' section?

 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
};

Any advice would be amazing! Many thanks

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

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

EXAMPLE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM