admin管理员组文章数量:1327259
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?
- possible duplicate of How to check if object has any properties in JavaScript? – Lightness Races in Orbit Commented Dec 10, 2011 at 20:51
2 Answers
Reset to default 6If 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
版权声明:本文标题:How can I test whether this Javascript object has any properties? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742210817a2433676.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论