2

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.

2
  • Could you give an example of how your object looks? Commented Sep 15, 2014 at 19:28
  • 1
    Can't you loop through them and break out of loop as soon as you hit an empty array? Commented Sep 15, 2014 at 19:34

1 Answer 1

5

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

Comments

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.