Question

If there are no records to display my getJSON response is as follows:

{
  "items": [],
  "total_count": 0
}

How can I access the "total_count" to code something like this:

$.each( data.items, function( key, value ) {
        if(data.items.length == 0) {
                      $('.list').html('');
                      $('.list').html('the ' + address + ' list is empty');  
                      return false; 
                      } else {
      // continue to display the returned records
   }

data.items.length

does not work I have tried == 0, == 1, > 1 < x and other variations with no success. I have also tried

if(!this["some_returned_item"])

and that does not work either. What I really want is if ("total_count" == 0) do something else do something else.

Any ideas guys? Many thanks in advance.

Was it helpful?

Solution

As the array is empty, the callback you pass to $.each isn't even called (it's called once for each item). You have to do the test out of this callback :

if (data.items.length === 0) {
      $('.list').html('the ' + address + ' list is empty');  
} else {
     $.each( data.items, function( key, value ) {
      // continue to display the returned records
     });
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top