How to delete a property of an object in JavaScript

How to delete a property of an object in JavaScript

1. delete

delete is the only real way to remove an object's properties without any leftovers, but it works 100 times slower than its "alternative" setting object[key] = undefined

var myObject = {
    "ircEvent": "PRIVMSG",
    "method": "newURI",
    "regex": "^http://.*"
};
delete myObject.regex;

console.log(myObject);

The delete operator removes the specified property from an object. Returns true if the deletion is successful, otherwise returns false. However, the following situations require important consideration:

If the property you are trying to delete does not exist, then delete will have no effect, but will still return true.

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

Any property declared with var cannot be deleted from the global scope or from the scope of a function.

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

Except for functions in the global scope, which 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 to non-settable using the Object.defineProperty() method cannot be deleted.

2. obj.field = undefined;

This choice is not the correct answer to this question! However, if you use it with care, you can speed up some algorithms significantly. If you use delete in a loop and have problems with performance, read the detailed explanation

 var obj = {
     field: 1     
 };
 obj.field = undefined;

3. Use delete in array

In an array, unlike a plain old object, where using delete leaves garbage in the form, null creates a "hole" in the array and the length remains unchanged.

var array = [1, 2, 3, 4];
delete array[2];
/* Expected result --> [1, 2, 4]
 * Actual result --> [1, 2, null, 4]
 */

4. Using splice in array

arrayObject.splice(index,howmany,item1,.....,itemX)

index: Required. Integer, specifies the position to add/remove items. Use negative numbers to remove items from the array.

The specified position at the end.

howmany: required. The number of items to delete. If set to 0, items will not be deleted.

item1: Optional. The new item added to the array.

var arr = new Array(6)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
arr[4] = "Adrew"
arr[5] = "Martin"

document.write(arr + "<br />")
arr.splice(2,0,"William")
document.write(arr + "<br />")
/*
*George, John, Thomas, James, Andrew, Martin
*George, John, William, Thomas, James, Andrew, Martin
*/

Summarize

This is the end of this article about how to delete a property of an object with JavaScript. For more information about how to delete a property of an object with JavaScript, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the idea of ​​implementing dynamic columns in angularjs loop object properties
  • JavaScript removes unnecessary properties of an object
  • When the springboot post interface accepts json, when it is converted to an object, the properties are all null.
  • Several ways to easily traverse object properties in JS
  • Use of hasOwnProperty method of js attribute object
  • The JS hasOwnProperty() method detects whether a property is an object's own property.
  • Parsing javascript Date object properties and methods through examples
  • Detailed explanation of dynamic addition, deletion, modification and query of properties when converting Java objects to JSON
  • When converting an object to json, java jackson ignores a property operation of the sub-object
  • Three properties of javascript objects

<<:  How to open external network access rights for mysql

>>:  How to install vim editor in Linux (Ubuntu 18.04)

Recommend

Install Mininet from source code on Ubuntu 16.04

Mininet Mininet is a lightweight software defined...

CSS Standard: vertical-align property

<br />Original text: http://www.mikkolee.com...

Detailed introduction to linux host name configuration

Table of contents 1. Configure Linux hostname Con...

Let's talk about the two functions of try catch in Javascript

The program is executed sequentially from top to ...

Detailed example of clearing tablespace fragmentation in MySQL

Detailed example of clearing tablespace fragmenta...

Solution to BT Baota Panel php7.3 and php7.4 not supporting ZipArchive

The solution to the problem that the PHP7.3 versi...

Example of using Nginx reverse proxy to go-fastdfs

background go-fastdfs is a distributed file syste...

Introduction to the use of anchors (named anchors) in HTML web pages

The following information is compiled from the Int...

Introduction to ApplicationHost.config (IIS storage configuration area file)

For a newly created website, take ASP.NET MVC5 as...

JavaScript implementation of classic snake game

This article shares the specific code of JavaScri...

Summary of some practical little magic in Vue practice

How can you forget lazy loading of routes that al...

vue3.0+echarts realizes three-dimensional column chart

Preface: Vue3.0 implements echarts three-dimensio...

An audio-visual Linux distribution that appeals to audiophiles

I recently stumbled upon the Audiovisual Linux Pr...

A brief discussion on the calculation method of key_len in mysql explain

The MySQL explain command can analyze the perform...