Home > Web Front-end > Front-end Q&A > How to delete object attributes in es6

How to delete object attributes in es6

青灯夜游
Release: 2023-01-11 09:19:17
Original
7735 people have browsed it

es6 Method to delete object attributes: use delete operator, syntax "delete object name.property name". Using the delete operator to delete an object property does not set the property value to undefined, but completely clears the specified property from the object.

How to delete object attributes in es6

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

Attributes are also called name-value pairs, including attribute names and attribute values. The attribute name can be any string including the empty string. There cannot be two attributes with the same name in an object. Attribute values ​​can be any type of data.

In js, if you want to delete object attributes, you can use the delete operator.

The delete operator can delete the properties of an object.

Example: Use the delete operator to delete the specified attribute.

var obj = {x : 1};  //定义对象
delete obj.x;  //删除对象的属性x
console.log(obj.x);  //返回undefined
Copy after login

When an object property is deleted, instead of setting the property value to undefined, the property is completely cleared from the object. If you use a for/in statement to enumerate object properties, only properties with a property value of undefined will be enumerated, but deleted properties will not be enumerated.

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to delete object attributes in es6. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
es6
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template