29

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.

Minko Gechev
  • 25,304
  • 9
  • 61
  • 68
Artpixler
  • 475
  • 3
  • 6
  • 8
  • 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. – Jared Aug 13 '13 at 03:28
  • possible duplicate of [Is object empty?](http://stackoverflow.com/questions/4994201/is-object-empty) – sierrasdetandil Oct 08 '13 at 00:48
  • Does this answer your question? [How do I test for an empty JavaScript object?](https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object) – mickmackusa Aug 04 '20 at 05:50

7 Answers7

77

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

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
14

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

$.isEmptyObject( object )
burakakkor
  • 335
  • 3
  • 12
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.

Minko Gechev
  • 25,304
  • 9
  • 61
  • 68
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;
};
stamat
  • 1,832
  • 21
  • 26
  • 1
    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. – Rogala Sep 19 '13 at 20:56
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');
} 
Fokwa Best
  • 3,322
  • 6
  • 36
  • 51
1

Dirty but simple and works:

function isEmptyObject(obj) {
  return JSON.stringify(obj) == '{}';
}
elixon
  • 1,123
  • 12
  • 15
-2
if (ricHistory.name2 === undefined) {
   //This is property has not been set (which is not really the same as empty though...)
}
Per Salbark
  • 3,597
  • 1
  • 23
  • 24