How to Remove a Property from a JavaScript Object

We used theObject.getOwnPropertyNamesmethod to get an array of the enumerable and non-enumerable properties of the object. An enumerable property is one that is added to the object using simple assignment, e.g. dot . Delete employee removes the property which name is contained inside name variable. Then just using Lodash unset method remove property from object. I have used Lodash “unset” to make it happen for a nested object also…

How can I remove a specific item from an array?

pop() function: This method is use to remove elements from the end of an array. shift() function: This method is use to remove elements from the start of an array. splice() function: This method is use to remove elements from the specific index of an array.

So, we have discussed a couple ways – the …rest syntax and reduce() method, to remove a property from an object without mutating it. This category is the oldest, most straightforward & most widely supported category of property removal. It supports Symbol & array indexes in addition to strings and works in every version of JavaScript except for the very first release. However, it’s mutative which violates some programming principles and has performance implications. It also can result in uncaught exceptions when used on non-configurable properties in strict mode. The delete operator is designed to be used on object properties. Using the object restructuring and rest syntax, we can destructure the object with the property to be removed and create a new copy of it.

Object destructuring with rest syntax

After applying the destructuring and rest syntax, restObject is going to contain the same properties as object, only without the removed property. Another approach to removing properties, but in an immutable manner without altering the original object, is to use the object destructuring and rest syntax. The delete operator does not directly free memory, and it differs from simply assigning the value of null or undefined to a property, in that the property itself is removed from the object.

If we don’t want to modify an object in-place, but also want a version of it without a specific property, we can just generate another object with all the same properties but the one. We used theArray.forEachmethod to iterate over the array and cleared each property using the deleteoperator. The first mutable approach is to use the delete operator.

Recommended reading:

You can also use other functions to achieve the same effect – omit, pick, … Similarly, removing the entire students array would be done by calling delete Hogwarts.students; or delete Hogwarts[‘students’];.

How do you delete and remove the elements from a dictionary?

  1. The del keyword.
  2. The clear() method.
  3. The pop() method.
  4. The popitem() method.

Assigning the result to the original object should do the trick . It is readable and short, however, it might not be the best choice if you are operating on a large number of objects as its performance is not optimized. I’m not saying it is, I’m provinding an alternative. Although delete used to have some performance implications, which I think are already described in other answers on this page. // Even when the property does not exist, delete returns “true”.

Array#splice(start[, deleteCount[, item1[, item2 ]]])

If deleteCount is omitted then elements from startIndex are removed to the end of the array. The delete operator removes a property from an object.

If a property with the same name exists on the object’s prototype chain, then after deletion, the object will use the property from the prototype chain. An object in JavaScript is a collection of key-value pairs. One of these key-value pairs is called an object property. Both keys and values of properties can be of any data type – Number, String, Array, Object, etc.

Rest-based string property omission

Alternative option but in an immutable manner without altering the original object, is using object destructuring and rest syntax. If a global property is configurable , it can be deleted, and subsequent references to them as global variables will produce a ReferenceError. This includes properties of built-in objects like Math, Array, Object and properties that are created as non-configurable with methods like Object.defineProperty().

  • Checking if obj exists before the && also makes sure you don’t throw an error due to calling the hasOwnProperty() function on an undefined object.
  • On each iteration, use the delete operator to delete the current property.
  • Memory is only freed by the garbage collector when all references to an object are removed.
  • In the following example, trees is removed with delete.
  • The property removal using delete operator is mutable because it mutates the original object.
  • Delete employee removes the property which name is contained inside name variable.
  • The delete operator is used to remove properties from objects.

The reputation requirement helps protect this question from spam and non-answer activity. Note that I purposely carried out more than one ‘delete’ operations in one loop cycle to minimize the effect caused by the other operations. A naive for loop will yield undefined for the value at the index.

Because classes are automatically in strict mode, and private properties can only be legally referenced in class bodies, this means private properties can never be deleted. While delete identifier may work if identifier refers to a configurable property of the global object, you should avoid this form and prefix it with globalThis instead. In this article, we have seen how to remove a property from an object in a few ways. We have seen that using delete will mutate the object.

Different built-in functions in JavaScript handle arrays with holes in them differently. @Loolooii you can rename the variable in your destructuring assignment to meet your argsIgnorePattern in the no-unused-vars rule.

Article was published on: 10/10/22

Author: Viktor Nikolaev

Victor is a professional crypto investor and stockbroker, specializing in such areas as trading on the stock exchange, cryptov currencies, forex, stocks and bonds. In this blog he shares the secrets of trading, current currency indices, crypt currency rates and tells about the best forex brokers. If you have any questions, you can always contact

Leave a Reply