user3475602
user3475602

Reputation: 1217

How to check if Object has empty arrays with underscore.js or jQuery

Is there a "better" way to check if an object has empty arrays (0-*) than this:

emptyArr: function() {
        var obj = getObj();
        return obj.abc.length == 0 || obj.def.length == 0 || obj.ghi.length == 0 || obj.jkl.length == 0 …………;
}

Edit: Here is how the object looks like:

- Object
  - abc = []
  - def = []
  - ghi = []
  - jkl = []
  - …

I want to check if the object contains any empty arrays.

Any help would be greatly appreciated.

Upvotes: 2

Views: 8180

Answers (1)

joews
joews

Reputation: 30340

Is the question "Check if an object has any empty array members"?

If so:

function hasEmptyArrays(obj) {
  var emptyArrayMembers = _.filter(obj, function(member) { 
    return _.isArray(member) && _.isEmpty(member)
  });

  return emptyArrayMembers.length > 0;
}

Upvotes: 5

Related Questions