Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does es6 determine whether an object contains an attribute

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to determine whether an object contains an attribute or not". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Two judgment methods: 1, using the in keyword, you can detect whether the object has a specified attribute, syntax "attribute name in object", if true is returned, it contains, and vice versa. 2. With the hasOwnProperty () function, the syntax "object .hasOwnProperty (property name)" is included if true is returned.

The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.

In es6, you can use methods such as indexOf (), includes (), and so on to check whether an array contains an element.

So how do you check the object? Determine if an attribute is included in the object?

Method 1: use the in keyword

Function: to detect whether an attribute exists in an object. You can use the in keyword to detect whether the current object has a specified attribute.

Syntax:

Attribute name in object

Determines whether the attribute name exists in the object and returns a Boolean value

Example:

Const person = {name: 'love', salary: 23}; console.log ('salary' in person); / / trueconsole.log (' sex' in person); / / false

Method 2: use the hasOwnProperty () function

You can determine whether an object contains a property name and return a Boolean value

Object .hasOwnProperty (property name)

Example:

Const person = {name: 'love', salary: 23}; person.hasOwnProperty ('salary') console.log (person.hasOwnProperty (' salary')); / / trueconsole.log (person.hasOwnProperty ('sex')); / / false

That's all for "how es6 determines whether an object contains an attribute". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report