javascript:检查对象是否具有特定元素或属性的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46425072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 06:39:38  来源:igfitidea点击:

javascript: best way of checking if object has a certain element or property?

javascriptobjectpropertiesundefined

提问by RocketNuts

Suppose I have this:

假设我有这个:

var person = { "name": "John Doe", "email": "[email protected]" };

This object only has two elements called nameand email. Some persons also have an element age, but this particular person doesn't. What's the best way to check this?

该对象只有两个元素,称为nameemail。有些人也有元素age,但这个特定的人没有。检查这个的最好方法是什么?

  • if (person.age) { ... }
  • if (person.age != undefined) { ... }
  • if (person.age !== undefined) { ... }
  • if (typeof(person.age) != 'undefined') { ... }
  • if (person.hasOwnProperty('age')) { ... }
  • if (person.age) { ... }
  • if (person.age != undefined) { ... }
  • if (person.age !== undefined) { ... }
  • if (typeof(person.age) != 'undefined') { ... }
  • if (person.hasOwnProperty('age')) { ... }

I know all these don't do the same, e.g. if (person.age)would also fail if agedoesexist but it's falseor nullor ''or 0. And I wonder if some aren't just flat out wrong.

我知道所有这些都不会做同样的事情,例如if (person.age),如果age确实存在但它是falseornull''or也会失败0。我想知道是否有些不只是完全错误。

Note that personis known to be an existing object here, but person.agemay or may not exist.

请注意,person此处已知为现有对象,但person.age可能存在也可能不存在。

回答by Rohit Agrawal

Let's check the reliability of these ways of checking if object has a certain element or property:

让我们检查这些检查对象是否具有某个元素或属性的方法的可靠性:

This can fail if Boolean(person.age)is false

如果Boolean(person.age)是,这可能会失败false

if (person.age) { ... }

This can fail if person.ageis nullor undefined

如果person.agenull或,这可能会失败undefined

if (person.age != undefined) { ... }

These can fail if person.ageis undefined

如果person.age是,这些可能会失败undefined

if (person.age !== undefined) { ... }
if (typeof(person.age) != 'undefined') { ... }

On the other hand, the hasOwnProperty()method returns a booleanindicating whether the object has the specified property as own (not inherited) property. So It does not depend on the value of person.ageproperty. So it is the best way here

另一方面,hasOwnProperty()方法返回一个boolean指示对象是否具有作为自己的(非继承的)属性的指定属性。所以它不取决于person.age财产的价值。所以这是这里最好的方式

if (person.hasOwnProperty('age')) { ... }

If you want to go further and check if an object has a property on it that is iterable(all properties including own properties as well as the inherited ones) then using for..inloop will give you the desired result.

如果您想进一步检查对象是否具有可迭代的属性(所有属性包括自己的属性以及继承的属性),那么使用for..in循环将为您提供所需的结果。

回答by Emir Maljanovic

You could use Object.hasOwnProperty(). Check out documentation here.

你可以使用Object.hasOwnProperty(). 在此处查看文档。

回答by mahip_j

You can use hasOwnProperty to check particular key or property present or not.

您可以使用 hasOwnProperty 来检查特定的键或属性是否存在。

var person = { "name": "John Doe", "email": "[email protected]" };
console.log(person.hasOwnProperty('age')) //false

var person = { "name": "John Doe", "email": "[email protected]", "age": 18 };
console.log(person.hasOwnProperty('age')) //true

if you use (person.age) it will give you false if the value is false or empty or null even the age property is present and has own property check property is present or not

如果你使用 (person.age) 它会给你 false 如果值是 false 或空或 null 即使年龄属性存在并且有自己的属性检查属性是否存在