How do I check if an object has a property in Java

2018-12-31 02:10发布

How do I check if an object has a property in JavaScript?

Consider:

x = {'key': 1};
if ( x.hasOwnProperty('key') ) {
    //Do this
}

Is that the best way to do it?

标签: javascript
23条回答
高级女魔头
2楼-- · 2018-12-31 02:41

You need to use the method object.hasOwnProperty(property). It returns true if the object has the property and false if the object doesn't.

查看更多
浅入江南
3楼-- · 2018-12-31 02:42

For testing simple objects use: if (obj[x] !== undefined)

If you don't know what object type it is use: if (obj.hasOwnProperty(x))

All other options are slower..

Details

Performance evaluation of 100,000,000 cycles under Nodejs to the 5 options suggested by others here:

function hasKey1(k,o) { return (x in obj); }
function hasKey2(k,o) { return (obj[x]); }
function hasKey3(k,o) { return (obj[x] !== undefined); }
function hasKey4(k,o) { return (typeof(obj[x]) !== 'undefined'); }
function hasKey5(k,o) { return (obj.hasOwnProperty(x)); }

The evaluation tells us that unless we specifically want to check the object's prototype chain as well as the object itself, we should not use the common form: if (X in Obj)... It is between 2 to 6 times slower depending on the use case

hasKey1 execution time: 4s 510.427785ms
hasKey2 execution time: 0s 904.374806ms
hasKey3 execution time: 0s 760.336193ms
hasKey4 execution time: 0s 935.19901ms
hasKey5 execution time: 2s 148.189608ms

Bottom line, if your Obj is not necessarily a simple object and you wish to avoid checking the object's prototype chain and to ensure x is owned by Obj directly, use 'if (obj.hasOwnProperty(x))...'.

Otherwise, when using a simple object and not being worried about the object's prototype chain, using if (typeof(obj[x]) !== 'undefined')... is the safest and fastest way.

If you use a simple object as a hash table and never do anything kinky, I would use if (obj[x])... as I find it much more readable.

Have fun.

查看更多
十年一品温如言
4楼-- · 2018-12-31 02:42

Do not do this object.hasOwnProperty(key)), its really bad because these methods may be shadowed by properties on the object in question - consider { hasOwnProperty: false } - or, the object may be a null object (Object.create(null)).

The best way is to do Object.prototype.hasOwnProperty.call(object, key) or:

const has = Object.prototype.hasOwnProperty; // cache the lookup once, in module scope.
/* or */
import has from 'has'; // https://www.npmjs.com/package/has
// ...
console.log(has.call(object, key));
查看更多
只靠听说
5楼-- · 2018-12-31 02:43

OK, it looks like I had the right answer unless if you don't want inherited properties:

if (x.hasOwnProperty('key'))

Here are some other options to include inherited properties:

if (x.key) // Quick and dirty, but it does the same thing as below.

if (x.key !== undefined)
查看更多
素衣白纱
6楼-- · 2018-12-31 02:43

hasOwnProperty "can be used to determine whether an object has the specified property as a direct property of that object; unlike the in operator, this method does not check down the object's prototype chain."

So most probably, for what seems by your question, you don't want to use hasOwnProperty, which determines if the property exists as attached directly to the object itself,.

If you want to determine if the property exists in the prototype chain you main want to use in, like:

if( prop in object ){ // do something }

I hope this helps.

查看更多
孤独总比滥情好
7楼-- · 2018-12-31 02:43

i used this. which is lots helped me if you have a objects inside object

if(typeof(obj["key"])=="string"){
    alert("property");
}
查看更多
登录 后发表回答