Home > Web Front-end > JS Tutorial > body text

How to delete an attribute from a javascript object

青灯夜游
Release: 2023-01-06 11:18:12
Original
20923 people have browsed it

In JavaScript, you can use the delete operator to delete the properties of an object. The syntax format is "delete object.property name;". When an object property is deleted, instead of setting the property value to undefined, the property is completely cleared from the object.

How to delete an attribute from a javascript object

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In JavaScript, objects are reference and composite data; object 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.

Delete attributes

Use the delete operator to delete the attributes of an object.

Example 1

The following example uses the delete operator to delete the specified attribute.

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

Output:

How to delete an attribute from a javascript object

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.

[Related recommendations: javascript learning tutorial]

The above is the detailed content of How to delete an attribute from a javascript object. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!