30

I have an object like this:

ricHistory = {
  name1: [{
    test1: value1,
    test2: value2,
    test3: value3
  }],
  name2: [{
    test1: value1,
    test2: value2,
    test3: value3
  }]
};

Now I want to check if e.g. name2 is empty with Javascript/jQuery. I know the method hasOwnProperty. It work for data.hasOwnProperty('name2') only if the name exists or not, but I have to check if its empty.

3
  • Your question asks how to check if an object is empty, yet the body of your question asks how to check if an array is empty. Please be more specific for future readers. Commented Aug 13, 2013 at 3:28
  • possible duplicate of Is object empty? Commented Oct 8, 2013 at 0:48
  • Does this answer your question? How do I test for an empty JavaScript object? Commented Aug 4, 2020 at 5:50

7 Answers 7

79

you can do this by jQuery.isEmptyObject()

Check to see if an object is empty (contains no properties).

jQuery.isEmptyObject( object )

Example:

jQuery.isEmptyObject({}) // true
jQuery.isEmptyObject({ foo: "bar" }) // false

from Jquery

Sign up to request clarification or add additional context in comments.

Comments

15

Another syntax from JQuery which is you are not using Prototype or similar and you prefer to use $ rather than jQuery prefix;

$.isEmptyObject( object )

Comments

9

Try this:

if (ricHistory.name2 && 
    ricHistory.name2 instanceof Array &&
    !ricHistory.name2.length) {
   console.log('name2 is empty array');
} else {
   console.log('name2 does not exists or is not an empty array.');
}

The solution above will show you whether richHistory.name2 exists, is an array and it's not empty.

1 Comment

Why not put all three checks in one, big, if?
5

Try this useful function:

function isEmpty(obj) {
if(isSet(obj)) {
    if (obj.length && obj.length > 0) { 
        return false;
    }

    for (var key in obj) {
        if (hasOwnProperty.call(obj, key)) {
            return false;
        }
    }
}
return true;    
};

function isSet(val) {
if ((val != undefined) && (val != null)){
    return true;
}
return false;
};

1 Comment

You could shorten isSet by doing this instead: function isSet(val) { return ((val != undefined) && (val != null));} I find it easier to just return the boolean of the statement instead of handing and then returning.
2

I go like this all the time in my code:

Assume my controller returns something like below:

$return['divostar'] = $this->report->get_additional_divostar_report($data);
$return['success'] = true;
return response()->json($return);

In Jquery, I would check like below:

if (jQuery.isEmptyObject(data.divostar)){
      html_result = "<p id='report'>No report yet</p>";
      $('#no_report_table').html(html_result).fadeIn('fast');
} 

Comments

1

Dirty but simple and works:

function isEmptyObject(obj) {
  return JSON.stringify(obj) == '{}';
}

Comments

-2
if (ricHistory.name2 === undefined) {
   //This is property has not been set (which is not really the same as empty though...)
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.