Javascript: How to test if response JSON array is empty

I'm getting back the following JSON:

{"array":[],"object":null,"bool":false}

And I'm testing it with the following, seemingly exhaustive, if statement:

$.ajax({
        type: "GET",
        url: "/ajax/rest/siteService/list",
        dataType: "json",
        success: function (response) {
            var siteArray = response.array;

            // Handle the case where the user may not belong to any groups
            if (siteArray === null || siteArray=== undefined || siteArray=== '' || siteArray.length === 0) {
                            window.alert('hi');
            }
       }
});

But the alert is not firing. :[

Answers


Use $.isArray() to check whether an object is an array. Then you can check the truthness of the length property to see whether it is empty.

if( !$.isArray(siteArray) ||  !siteArray.length ) {
    //handler either not an array or empty array
}

Need Your Help

Checking next Line to then implement where the line should go to

python pseudocode

I'm having trouble figuring out how to implement having a text file with lines having both lines of words and then lines of numbers. What I want to try and do is read the lines under the words then...

struts checkbox problem

java checkbox struts

i have some checkbox in my jsp page. there are some othe textfields in the jsp page. i can populate the value of textboxs from database. like for name field i can fetch the value 'myname' from data...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.