JavaScript

How to Remove a Property From a JavaScript Object

In JavaScript, an object is a data type that refers to the collection of key-value pairs. Objects can be used to store and manipulate data in a structured way. Sometimes, developers need to delete some properties from an object to maintain the structure. JavaScript offers some built-in methods to perform these tasks.

This post will describe the method for removing the property from the selected object.

Remove/Eliminate a Property From an Object

For the purpose of eliminating a property from an object, use the following approaches:

Method 1: Remove/Eliminate a Property From an Object Using “delete” Operator

You can utilize the “delete” operator for eliminating a property from a particular object. More specifically, you must repeat the delete operator in the same function if you want to delete several properties.

Syntax

Follow the provided syntax to delete the property from a JavaScript object:

delete object.property

Or

delete object['property'];

Example

Create an object:

let object = {

  name: 'John',

  age: 30,

  rollno: 15

};

Use the delete operator to delete the property “rollno” of an object:

delete object.rollno;

It can be seen that the “rollno” has been successfully deleted from the specified object:

Method 2: Remove/Eliminate a Property From a JavaScript Object Using filter() Method

You can also use the “filter()” method to remove a property from an object in JavaScript. It makes a new array with elements that satisfy a function’s condition.

Syntax

Follow the mentioned syntax for the filter() method:

array.filter(function => (currentValue, index, array))

Example

Create an object called “info”:

let info = {

  firstName: 'John',

  lastName: 'Cove',

  age: 27,

  rollno: 18

};

Now, create an empty object called “filteredObj” that contains the values that pass the given condition:

let filteredObj = {};

Invoke the filter() method with the keys of the object, and fetch the properties that are not equal to the key “lastName”, and store it in an empty object:

Object.keys(info).filter(property => {

if (property !== 'lastName') {

  filteredObj[property] = info[property]

}

})

Print the filtered object on the console:

console.log(filteredObj);

It can be observed that the property “lastName” has been deleted from the resultant object:

Method 3: Remove/Eliminate a Property From a JavaScript Object Using Spread Operator

Another approach to remove a property from a JavaScript object is to use the “spread operator”. It copies all of the properties except the specified property that has been deleted from the object.

Syntax

Use the following syntax to remove a property from a JavaScript object using spread operator:

const { deletedProperty, ...restObjectProperties } = object

Example

Create an object called “infoObject”:

let infoObject = {

  name: 'John',

  age: 30,

fieldOfInterest: "JavaScript"

};

Create a new object that does not contain the property “age”:

const { age, ...restObj } = infoObject;

Print the new object called “restObj” on the console with the help of the “console.log()” method:

console.log(restObj);

Output

That’s all about removing the property from a JavaScript object.

Conclusion

For removing or eliminating a property from a JavaScript object, utilize the “delete” operator, “filter()” method, or the “Spread operator (…)”. However, the “delete” operator is the easiest and most commonly used approach for removing the property from JavaScript objects. This post described several approaches for removing the property from the selected object.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.