0
{ "questions": [
  {
   "text": "Which colors do you see in the rainbow?",
   "answer": "A",
   "A": "RED",
   "B": "ORANGE",
   "C": "YELLOW",
   "D": "GREEN"
 },
 {
  "text": "What is sanatbek's family name?",
  "answer": "C",
  "A": "Saidov",
  "B": "Muhammadlatipov",
  "C": "Matlatipov",
  "D": "Boboyev"
 },
 {
  "text": "How many members does this company have?",
  "answer": "C",
  "A": "3",
  "B": "8",
  "C": "9",
  "D": "2"
 }
 ]
}

This is my JSON file and I need to get length of questions in AngularJS like this:

function getQuestionCount() {
    return $scope.questions.length;
}

Here $scope.questions is declared in the controller: I have tried several solutions like

  1. return Object.keys($scope.questions).length this is returning value: 6; after displaying console.log($scope.questions) . I understood that this was number of properties of my Parsed Object.
  2. Then after thinking a bit, I tried this return $scope.questions.data.questions.length; this is working. However, Following error is showing on my console.

Here is My Error Message:

Error message

TypeError: Cannot read property 'data' of undefined.
2
  • What is $scope.questions? Is it the array of three objects? Commented Oct 28, 2017 at 7:20
  • Yes it should be the array of three objects which is taken from questions key. Mr Raghu's solution is the answer for my problem. Commented Oct 28, 2017 at 9:35

3 Answers 3

1

From the ScreenShot you shared it is understood you made a HTTP GET call to retrieve the data. After your call gets successfully completed you can set the questions length in a variable in scope.

$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    $scope.questionsLenght = response.data.questions.length;
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. When I parsed from JSON I took full response instead of response.data.questions to my global variable!
0

if in your controller you set your json string as

  $scope.questions = {
  "questions": [
  {
      "text": "Which colors do you see in the rainbow?",
      "answer": "A",
      "A": "RED",
      "B": "ORANGE",
      "C": "YELLOW",
      "D": "GREEN"
  },
 {
     "text": "What is sanatbek's family name?",
     "answer": "C",
     "A": "Saidov",
     "B": "Muhammadlatipov",
     "C": "Matlatipov",
     "D": "Boboyev"
 },
 {
     "text": "How many members does this company have?",
     "answer": "C",
     "A": "3",
     "B": "8",
     "C": "9",
     "D": "2"
 }
    ]
}; 

try

 $scope.questions.questions.length;

as the name of array inside your json string is questions

Comments

0

you can get length by using for loop:

for (var i = 0; i < $scope.questions.length; i++) {
   $scope.data.push(angular.copy(types[i]));
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.