Javascript 如何检查对象是否为空或填充了空道具

Javascript 如何检查对象是否为空或填充了空道具,javascript,object,Javascript,Object,如何发现对象是否为空,即使它具有属性 e、 g 我尝试了什么: function isEmpty(data) { return !_.isEmpty(data); } var obj1 = {}; var obj2 = {"oneProp": {"subProp": []}}; console.log(isEmpty(obj1)); //true console.log(isEmpty(obj2)); // false but should be true 问题: function

如何发现对象是否为空,即使它具有属性

e、 g

我尝试了什么:

function isEmpty(data) {
   return !_.isEmpty(data);
}

var obj1 = {};
var obj2 = {"oneProp": {"subProp": []}};

console.log(isEmpty(obj1)); //true
console.log(isEmpty(obj2)); // false but should be true
问题:

function isEmpty(data) {
   return !_.isEmpty(data);
}

var obj1 = {};
var obj2 = {"oneProp": {"subProp": []}};

console.log(isEmpty(obj1)); //true
console.log(isEmpty(obj2)); // false but should be true
有什么好方法可以在不知道每个可能属性的名称的情况下检查对象是否为空

我发现了什么,但可能不是最好的解决方案:

function isEmpty(data) {
   return !_.isEmpty(data);
}

var obj1 = {};
var obj2 = {"oneProp": {"subProp": []}};

console.log(isEmpty(obj1)); //true
console.log(isEmpty(obj2)); // false but should be true
这是我的问题的简单解决方案,但这是在更大的对象中复杂化的方法

var isEmpty = true;
for (var property in object) {
    if (object.hasOwnProperty(property)) {            
        // check if prop is array or 
        // object and go deeper again
        // else check for prop.length
        // set isEmpty to false if > 0
    }
}
还有其他更人性化的方式吗

console.log(isEmpty(obj2)); // false but should be true
这根本不是事实

如果

然后,由于
subop
,obj2实际上根本不是空的


然后是的,您必须重复遍历对象以检查每个属性。

您需要使用递归函数遍历对象。见下文:

var obj1 = {};  //empty
var obj2 = {"oneProp": {"subProp": []}}; //empty
var obj3 = {"a": {"b": [], c: "not empty"}}; //not empty
var obj4 = {"very": {"deeply": { "nested": {"object": [] } } } } //empty

function isEmptyRecurse(obj) {
    return _.isEmpty(obj) || ( _.isObject(obj) && _.every(_.values(obj),isEmptyRecurse) )
}


console.log(isEmptyRecurse(obj1)); //true
console.log(isEmptyRecurse(obj2)); //true
console.log(isEmptyRecurse(obj3)); //false
console.log(isEmptyRecurse(obj4)); //true

这样的函数将帮助您快速检查对象是否至少有一个非空属性。此函数将获取任何对象并检查每个(深/嵌套)属性,以确定obj是否为空。我们在第一次出现“非空”属性时停止,以节省时间

function isEmpty(obj) {
    var res = true;
    for (var prop in obj) {
        if (! obj.hasOwnProperty(prop)) { continue; }
        var type = typeof obj[prop];

        switch (type){
            case "object":
                res = isEmpty(obj[prop]);
                break;

            case "boolean":
            case "number":
                res = false; // boolean cannot be "empty", also 0 is not empty
                break;

            case "string":
                res = ! obj[prop].length;
                break;

            case "undefined":
                res = true;
                break;

            default:
                res = !! obj[prop];
                break;
        }
        if (!res) {break;}
    }
    return res;
}

var obj1 = {"oneProp": {"subProp": [], "test": ""}};
var obj2 = {"oneProp": {"subProp": [], "test": "1"}};

alert( isEmpty(obj1) )
alert( isEmpty(obj2) )
但是,这种方法相对较慢(hasOwnProperty检查是主要的瓶颈)。如果您需要经常执行此检查,或者有复杂的对象,我会以某种方式缓存结果。可能是这样的:

var _cache = {};
function isEmpty(obj) {
  // Try to get the result from the cache.
  var json = JSON.stringify(obj);
  if (undefined !== _cache[json]) { 
    return _cache[json]; 
  }

  // here is the code from above...

  _cache[json] = res; // Add the result to the cache.
  return res;
}

基本上就是这样。您必须递归地遍历对象图,查找具有值的属性。没有其他方法。你写的就是解决方案。对于更大的对象,你只需要让方法调用它自己。我的意思是,在我的例子中,而不是在编程中:)我是在我的问题上说的