1. deletedelete 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 arrayIn 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 arrayarrayObject.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 */ SummarizeThis 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:
|
<<: How to open external network access rights for mysql
>>: How to install vim editor in Linux (Ubuntu 18.04)
Table of contents MyISAM and InnoDB Reasons for p...
Preface Nginx 's built-in module supports lim...
An at-rule is a declaration that provides instruc...
Preface Review and summary of mobile terminal rem...
When the token expires, refresh the page. If the ...
Although Microsoft has done a lot of research and ...
1. Tools We need two tools now: MySQL server (mys...
MySQL stored procedures, yes, look like very rare...
1. Background The following two problems are enco...
The command to delete images in docker is docker ...
After being tortured by the front-end cross-domai...
MySQL and connection related timeouts Preface: To...
1.v-bind (abbreviation:) To use data variables de...
As a technical novice, I am recording the process...
This article introduces common problems of Xshell...