Is it possible to add dynamically named properties to JavaScript object?

后端 未结 19 1470
攒了一身酷
攒了一身酷 2020-11-21 05:39

In JavaScript, I\'ve created an object like so:

var data = {
    \'PropertyA\': 1,
    \'PropertyB\': 2,
    \'PropertyC\': 3
};

Is it poss

19条回答
  •  野的像风
    2020-11-21 06:15

    You can add as many more properties as you like simply by using the dot notation:

    var data = {
        var1:'somevalue'
    }
    data.newAttribute = 'newvalue'
    

    or:

    data[newattribute] = somevalue
    

    for dynamic keys.

提交回复
热议问题