Check for JSON array

I know that at first glance it sounds like a repeating question, but I don’t think it ...

I get a JSON array as:

var test1 = [] ; 

or

 var test2 = [{},{},{}] ; //This is empty 

I have no problem finding if test1 empty.

 jQuery.isEmptyObject(test1) 

My problem is with test2 ... Please note that in some cases test2 may return something like:

 var test2 = [{"a":1},{},{}] ; //All these are not empty var test2 = [{},{"a":1},{}] ; //All these are not empty var test2 = [{},{},{"a":1}] ; //All these are not empty 

The above scenarios should not be considered empty. I tried to use .length , but that does not help, since the length is always 3 ... Any ideas?

Greetings.

+7
javascript jquery
source share
8 answers
 function isArrayEmpty(array) { return array.filter(function(el) { return !jQuery.isEmptyObject(el); }).length === 0; } 

jsFiddle Demo

Skips all your tests.

A !jQuery.isEmptyObject(el) JavaScript solution would be to replace !jQuery.isEmptyObject(el) with Object.keys(el).length !== 0

Edit: using Array.prototype.every

 function isArrayEmpty(array) { return array.every(function(el) { return jQuery.isEmptyObject(el); }); } 
+7
source share

For those who play at home, a solution without jQuery:

 var test2 = [{a: 1},{},{}] ; //This is empty function isEmpty(val) { var len = val.length, i; if (len > 0) { for (i = 0; i < len; ++i) { if (!emptyObject(val[i])) { return false; } } } return true; } function emptyObject(o) { for (var key in o) { if (o.hasOwnProperty(key)) { return false; } } return true; } console.log(isEmpty(test2)); 
+2
source share

Without jQuery: using Array.filter 1 and Object.keys 2 :

 function JSONEmpty(obj){ return !obj.length || !obj.filter(function(a){return Object.keys(a).length;}).length; } // usage JSONEmpty([{"a":1},{},{}]); //=> false JSONEmpty([{},{"a":1},{}]); //=> false JSONEmpty([{},{},{"a":1}]); //=> false JSONEmpty([]); //=> true JSONEmpty([{},{},{}]); //=> true 

update 2018 Arrow functions are now supported by all modern browsers, so himel-nag-rana , you can also use:

 const JSONEmpty = obj => !obj.length || !obj.filter(a => Object.keys(a).length).length; 

1 Additional Information
2 Additional information (links contain pads for older browsers)

+1
source share

I had the same problem and came with this solution without jQuery:

 function isEmpty(x) { for(var i in x) { return false; } return true; } 
+1
source share

Pretty simple ...

 if(jQuery.isEmptyObject(test2[0]) && jQuery.isEmptyObject(test2[1]) && jQuery.isEmptyObject(test2[2]))) // do something 
0
source share

Perhaps you could try using a function like

 function isEmptyObject (test) { for (var i in test) { if (!jQuery.isEmptyObject(test[i]) return false; } return true; } 
0
source share

Here is my trick: turn the array into a set and check the size.

 var myArray = [1,2,3]; var mySet = new Set(myArray); console.log(mySet.size === 0); 
0
source share

check by cyclizing each value in the array and return errors Try

 for(i=0;js_array[i]!=null;i++) { if(js_array[i]=="") { alert("Empty"); } } 
-one
source share

All Articles