Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is my JavaScript Object:

var data = {

    'PropertyA': 1,

    'PropertyB': 2,

    'PropertyC': 3

};

When the property name is not determined until runtime like below, can I add further properties to the object? 

var propName = 'Property' + someUserInput

//imagine someUserInput was 'Z', how can I now add a 'PropertyZ' property to 

//my object?

1 Answer

0 votes
by (19.7k points)

You can do it like below:

var data = {

    'PropertyA': 1,

    'PropertyB': 2,

    'PropertyC': 3

};

data["PropertyD"] = 4;

// dialog box with 4 in it

alert(data.PropertyD);

alert(data["PropertyD"]);

Interested in Java? Check out this Java tutorial by Intellipaat.   

Browse Categories

...