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

Complete MySQL Learning Notes

Table of contents MyISAM and InnoDB Reasons for p...

Implementing access control and connection restriction based on Nginx

Preface Nginx 's built-in module supports lim...

Summary of @ usage in CSS (with examples and explanations)

An at-rule is a declaration that provides instruc...

Example of how rem is adapted for mobile devices

Preface Review and summary of mobile terminal rem...

Detailed steps to install mysql5.7.18 on Mac

1. Tools We need two tools now: MySQL server (mys...

Summary of MySQL stored procedure permission issues

MySQL stored procedures, yes, look like very rare...

Two problems encountered when deploying rabbitmq with Docker

1. Background The following two problems are enco...

How to delete an image in Docker

The command to delete images in docker is docker ...

Detailed summary of MySQL and connection-related timeouts

MySQL and connection related timeouts Preface: To...

A brief discussion on VUE uni-app template syntax

1.v-bind (abbreviation:) To use data variables de...

Linux CentOS6.9 installation graphic tutorial under VMware

As a technical novice, I am recording the process...

Detailed explanation of Xshell common problems and related configurations

This article introduces common problems of Xshell...