admin管理员组

文章数量:1327453

I have some code that adds properties to an object like this:

var MyObject = new Object();

if (....) { MyObject.prop1 = .... ; }
if (....) { MyObject.prop2 = .... ; }
if (....) { MyObject.prop3 = .... ; }

After going through all these if statements, I want to test and see whether MyObject has properties. I don't want to test if it has prop1 || prop2 || prop3 because there are 12 properties that can be added.

How do I test to see if it has at least one? I tried if (MyObject) but of course that doesn't work because that only tests the existence of the object. Is these something like a simple one-liner like (MyArray.length) for objects?

I have some code that adds properties to an object like this:

var MyObject = new Object();

if (....) { MyObject.prop1 = .... ; }
if (....) { MyObject.prop2 = .... ; }
if (....) { MyObject.prop3 = .... ; }

After going through all these if statements, I want to test and see whether MyObject has properties. I don't want to test if it has prop1 || prop2 || prop3 because there are 12 properties that can be added.

How do I test to see if it has at least one? I tried if (MyObject) but of course that doesn't work because that only tests the existence of the object. Is these something like a simple one-liner like (MyArray.length) for objects?

Share Improve this question edited Dec 10, 2011 at 20:51 Lightness Races in Orbit 385k77 gold badges666 silver badges1.1k bronze badges asked Dec 10, 2011 at 20:31 frenchiefrenchie 52k117 gold badges319 silver badges527 bronze badges 1
  • possible duplicate of How to check if object has any properties in JavaScript? – Lightness Races in Orbit Commented Dec 10, 2011 at 20:51
Add a ment  | 

2 Answers 2

Reset to default 6

If you're working in a pre-ECMAScript5 environment, it's easy enough with a small loop:

var found = false, name;
for (name in MyObject) {
    if (MyObject.hasOwnProperty(name)) {
        found = true;
        break;
    }
}

Or since you're using var MyObject = new Object(); to create it (BTW, you can just use var MyObject = {};), you don't actually need the hasOwnProperty call:

var found = false, name;
for (name in MyObject) {
    found = true;
    break;
}

for..in loops through the enumerable properties of an object, both its own and ones it inherits from its prototype; hasOwnProperty tells you whether the property es from the object itself or its prototype. All of the properties you're adding will be enumerable, so they'll show up in the loop. Raw objects ({}) have no enumerable properties initially unless someone has been mucking about with Object.prototype (which is a Really Bad Idea), hence the second loop above.

ECMAScript5 (released a couple of years ago, not supported by older browsers and support varies a bit even in more recent ones) adds a couple of functions you could use for this. Probably the most relevant is Object.keys, which returns an array of the names of the object's "own" properties (not properties it inherits from its prototype). That would mean you wouldn't need the loop:

if (Object.keys(MyObject).length) { // Only on ES5-pliant browsers
    // Yes it has at least one
}

Try

Object.getOwnPropertyNames(MyObject).length; 

This will give you number of object's own properties.

See documentation.

Object.getOwnPropertyNames returns an array whose elements are strings corresponding to the enumerable and non-enumerable properties found directly upon obj.

If you want to support older browsers that don't have Object.getOwnPropertyNames use this:

var getPropertyNames = Object.getOwnPropertyNames || function(obj) {
  var propNames = [];
  for (var propName in obj) {
    if(obj.hasOwnProperty(propName)) {
      propNames.push(propName);
    }
  }
  return propNames;
}  

getPropertyNames(MyObject).length; // number of own properties

本文标签: How can I test whether this Javascript object has any propertiesStack Overflow