简体   繁体   中英

Check if a JSON array is empty

I know from the first look it sounds like duplicate question but i don't think it is...

I am receiving back a JSON array as:

var test1 = [] ;

or

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

I have no problem finding out if test1 is empty.

jQuery.isEmptyObject(test1)

My problem is with the test2 ... Please note that in some cases the test2 might 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 shouldn't be counted as empty.I've tried to use .length but it's not helping as the length is always 3... Any ideas?

Cheers.

function isArrayEmpty(array) {
    return array.filter(function(el) {
        return !jQuery.isEmptyObject(el);
    }).length === 0;
}

jsFiddle Demo

Passes all of your tests.

A pure 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);
    });
}

For those playing at home, a non jQuery solution:

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));

Without JQuery: using Array.filter and Object.keys : Object.keys

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 like himel-nag-rana stipulated, you can also use:

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

More info 更多信息
More info (links contain shims for older browsers) 更多信息 (链接包含旧版浏览器的填充程序)

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

function isEmpty(x) {
   for(var i in x) {
       return false;
   }
   return true;
}

Pretty simple...

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

Maybe you could try use function like

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

Here's my take: turn the array into a set and check for size.

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

check by looping each values in array and return error Try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM