roy
roy

Reputation: 39

Response array of success call back length is undefined

I am getting the response through through ajax jQuery am trying to get the length of the the array its showing as undefined . Here is the code

data.json file

{
  "response": [{
    "id": 1,
    "name": "Web Demo"
  }, {
    "id": 2,
    "name": "Audio Countdown"
  }, {
    "id": 3,
    "name": "The Tab Key"
  }, {
    "id": 4,
    "name": "Music Sleep Timer"
  }, {
    "id": 5,
    "name": "Music Sleep Timer"
  }]
}  

and my js file code is:

$.ajax({
  url: "data.json",
  type: "GET",
  dataType: "json",
  success: function(response) {
    console.log(response);
    console.log(response.length);
  }
});  

I am trying to print the length of response array its showing undefined instead of length 5 so can anyone please help how to get the length of response length

Upvotes: 0

Views: 79

Answers (1)

Pranav C Balan
Pranav C Balan

Reputation: 115232

You are checking length property of an object which will be undefined, so instead you need to check the length of inner array.

console.log(response.response.length)
// get array by -----^------

Upvotes: 1

Related Questions