How to remove property from Object in JavaScript?

January 04, 2020 No comments QA JavaScript Property

1. Introduction

In this article, we will show a way to properly remove a particular element from an array in JavaScript. The best method to do this, is to use JS delete operator.

2. Delete operator

The delete operator was designed to remove given property from an object in a hard way. There will not be a way to restore it again. On success delete will return true, otherwise false will be returned. Keep in mind that if a property doesn't exist, the delete operator will also return true.

In the following example, we present how to use delete command to remove permanently property from an Object.

var sample = {
    "ip": "127.0.0.1",
    "method": "POST",
    "action": "request"
};

// delete ip property 
delete sample.ip;

// or,
delete sample['ip'];

// or,
var prop = "ip";
delete sample[prop];

console.log(sample);

output:

{
  action: "request",
  method: "POST"
}

3. Conclusion

The article showcased how to delete property from an Object in JavaScript. The delete operator seems to be the best way to achieve that and we recommend using it.

{{ message }}

{{ 'Comments are closed.' | trans }}