Home  >  Article  >  Web Front-end  >  How to delete key-value pairs in javascript

How to delete key-value pairs in javascript

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-07-20 14:17:339518browse

In JavaScript, you can use the delete operator to delete key-value pairs, and the syntax format is "delete object name.property". The delete operator is used to delete a property of an object; if there is no reference to the property, it will eventually be released.

How to delete key-value pairs in javascript

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

The delete operator is used to delete an attribute of an object; if there is no reference to this attribute, it will eventually be released.

Syntax:

delete expression
// expression 的计算结果应该是某个属性的引用,例如:
delete object.property
delete object['property']

object: The name of the object, or an expression that evaluates to an object.

property: The property to be deleted.

True in all cases, unless the property is a non-configurable property of its own, in which case non-strict mode returns false.

In strict mode, if the property is a property that cannot be configured by itself, a TypeError will be thrown.

The delete operator removes the specified attribute from an object. It will return true when deleted successfully, otherwise it will return false.

However, the following situations need to be considered:

  • If the attribute you are trying to delete does not exist, delete will have no effect, but will still return true

  • If there is an attribute with the same name as the attribute to be deleted on the prototype chain of the object, then after deleting the attribute, the object will use that attribute on the prototype chain (that is, the delete operation will only Works on its own properties)

  • Any property declared using var cannot be deleted from the global scope or the scope of the function.

    • In this case, the delete operation cannot delete any function in the global scope (whether the function comes from a function declaration or a function expression)

    • Except that functions in the global scope cannot be deleted, functions in objects can be deleted using the delete operation.

  • Any property declared with let or const cannot be deleted from the scope in which it is declared.

  • Non-configurable properties cannot be removed. This means that properties of built-in objects like Math, Array, Object and properties set as unsettable using the Object.defineProperty() method cannot be deleted.

Take object json as an example:

var json = {
name:'张三',
age:'23'
};

How to delete the age attribute in json

delete  json.age;

[Recommended learning: javascript advanced tutorial

The above is the detailed content of How to delete key-value pairs 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