最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if Object has empty arrays with underscore.js or jQuery - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Sep 15, 2014 at 19:32 user3475602 asked Sep 15, 2014 at 19:25 user3475602user3475602 1,2172 gold badges22 silver badges43 bronze badges 2
  • Could you give an example of how your object looks? – Afsa 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? – Huangism Commented Sep 15, 2014 at 19:34
Add a ment  | 

1 Answer 1

Reset to default 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;
}
发布评论

评论列表(0)

  1. 暂无评论