Home  >  Article  >  Web Front-end  >  How to delete object attributes in javascript

How to delete object attributes in javascript

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-04-08 17:26:4230559browse

JS method to delete object properties: 1. Use the deleteProperty method, the syntax "Reflect.deleteProperty (array name, "name of the element object")"; 2. Use the delete keyword, the syntax "delete array name. Attributes".

How to delete object attributes in javascript

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

Method 1: Use the deleteProperty static method of the Reflect object in ES6

How to delete object attributes in javascript##

var a = {aa: 1,bb:2}
Reflect.deleteProperty(a, 'aa')

method 2: Using the delete keyword

delete obj.age
console.log(obj) // { name: 'xiaoming', id: 2 }

delete can only delete the attributes on the object, but not the defined variables, and this Operator/behavior is very slow

[Recommended learning:

javascript video tutorial

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

Statement:
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