Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if JSON object is empty in jQuery

I have the following JSON:

{
    "meta": {
        "limit": 20,
        "next": null,
        "offset": 0,
        "previous": null,
        "total_count": 0
    },
    "objects": []
}

I'm interested in objects: I want to know if objects is empty and show an alert:

something like this:

success: function (data) {
    $.each(data.objects, function () {
        if data.objects == None alert(0)
        else :alert(1)
    });
like image 566
GrantU Avatar asked Nov 28 '22 07:11

GrantU


1 Answers

i don't know what is you meaning about empty object, but if you consider

{}

as a empty object, i suppose you use the code below

var obj = {};

if (Object.keys(obj).length === 0) {
    alert('empty obj')
}
like image 88
GilbertSun Avatar answered Nov 30 '22 20:11

GilbertSun