Paul
Paul

Reputation: 97

check that a collection is not empty

How do you check that data.results in the following is not empty before trying to perform actions on it?

$.getJSON(myurl, function(data) {
  // if data.results is not empty 
  // {
  //    console.log(data.results.size);
  // }

});

Upvotes: 3

Views: 13606

Answers (3)

Carls Jr.
Carls Jr.

Reputation: 3078

how about doing this one?

if(data!=undefined){
// do logic here
}

Upvotes: 0

InvisibleBacon
InvisibleBacon

Reputation: 3222

I usually use something like this:

if (data != null && data.results != null && data.results.size != 0) ...

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1038880

if (data != null && data.results != null && data.results.length > 0) {
    // the array is not empty
}

Upvotes: 9

Related Questions