3 Methods to Remove a Property from an Object in JavaScript

Posted by


In JavaScript, objects are data structures that store key-value pairs. There are different ways to delete a property from an object, depending on the specific use case and requirements. In this tutorial, we will explore three ways to delete a property from an object in JavaScript.

  1. Using the delete keyword:
    The simplest way to delete a property from an object in JavaScript is by using the delete keyword. The syntax for deleting a property is as follows:

    let obj = { key1: value1, key2: value2, key3: value3 };
    delete obj.key2;

    In this example, we have an object ‘obj’ with three properties: key1, key2, and key3. We can delete the ‘key2’ property using the delete keyword. After deleting the property, the object will be updated to:

    { key1: value1, key3: value3 }

    It is important to note that the delete keyword does not remove the property from the object entirely but sets it to undefined. This means that if you try to access the deleted property, you will get ‘undefined’ as the value.

  2. Using Object.assign():
    Another way to delete a property from an object in JavaScript is by using the Object.assign() method. This method creates a new object by copying properties from one or more source objects. We can use this method to create a new object without the property we want to delete. The syntax for deleting a property using Object.assign() is as follows:

    let obj = { key1: value1, key2: value2, key3: value3 };
    let newObj = Object.assign({}, obj);
    delete newObj.key2;

    In this example, we create a new object ‘newObj’ by copying all the properties from ‘obj’ using Object.assign(). We then delete the ‘key2’ property from newObj. After deleting the property, ‘newObj’ will not contain the ‘key2’ property.

  3. Using the spread operator (…) in ES6:
    With the introduction of ES6, a new syntax called the spread operator (…) was added to JavaScript. This operator allows us to spread the properties of an object into a new object. We can use the spread operator to delete a property from an object by spreading all properties except the one we want to delete. The syntax for deleting a property using the spread operator is as follows:

    let obj = { key1: value1, key2: value2, key3: value3 };
    let { key2, ...newObj } = obj;

    In this example, we create a new object ‘newObj’ by spreading all properties of ‘obj’ except for ‘key2’. After deleting the property, newObj will not contain the ‘key2’ property.

In conclusion, there are multiple ways to delete a property from an object in JavaScript. The delete keyword, Object.assign() method, and the spread operator (…) in ES6 provide different ways to achieve this. Choose the method that best fits your requirements and coding style.

0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x