Suppose I have a javascript Object
var MyObject= { "Name": "Value1", "Email": "test@gmail.com" };
And now I want to add a new property or remove a property from the above object, how can I achieve it in javascript?
you can delete object using delete
Example:
delete MyObject.Name;
// or,
delete MyObject['Name'];
// or,
var prop = "Name";
delete MyObject[prop];
Another way to delete an object is to undefined it
obj.field = undefined;
//use this approach when you care about performance
Adding a property to object
Adding a proerty is easy, if you know the basics of Javascript Objects
MyObject.NewProperty = "NewValue";
//you can get the value using
alert(MyObject.NewProperty);
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly