8

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.

4
  • Are you saying that it should only return true if the array is empty or the array only contains objects without keys? Commented Aug 23, 2013 at 6:26
  • Why not just use this if statement: test2[0].length===0&&test[1].length===0&&test[2].length===0 i know it s crude but whatever Commented Aug 23, 2013 at 6:30
  • 1
    length is not a valid property on an object. Commented Aug 23, 2013 at 6:31
  • +1 Evan, if you use .length to an object you will just get an undefined error . @ Evan Trimboli i just need to capture the scenario were test2 is [{},{},{}] Commented Aug 23, 2013 at 6:35

8 Answers 8

7
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);
    });
}
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

2

Without JQuery: using Array.filter1 and Object.keys2:

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;

1 More info
2 More info (links contain shims for older browsers)

4 Comments

Probably worth noting the browser compatibility for Object.keys and Array.filter.
a more shorter version with arrow function isJSONArrayEmpty(obj) { return !obj.length || !obj.filter(a => Object.keys(a).length).length; }
@HimelNagRana I'm pretty sure arrow functions were not or partly implemented in most browsers back in august 2013
@KooiInc, yes of course that came much later. But my point of adding this was just to add to the solution so that when devs from 2018 comes they get a shorter/updated version. Nothing else. :-)
1

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

Comments

0

Pretty simple...

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

2 Comments

What if the array has 4 elements?
Yeah i was lazy but you could very easily implement this in a loop.
0

Maybe you could try use function like

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

Comments

0

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

Comments

-1

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

1 Comment

This will always return false since the condition is using an assignment to a falsey value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.