how to add-remove property in Javascript Object?


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?


Asked by:- pika
1
: 136 At:- 6/20/2017 3:17:39 PM
javascript add-remove javascript-objects






1 Answers
profileImage Answered by:- Vipin

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);

0
At:- 6/21/2017 8:21:04 AM





Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use