Appending to an object

后端 未结 13 1541
心在旅途
心在旅途 2020-11-27 10:19

I have an object that holds alerts and some information about them:

var alerts = { 
    1: { app: \'helloworld\', message: \'message\' },
    2: { app: \'hel         


        
13条回答
  •  鱼传尺愫
    2020-11-27 11:15

    You should really go with the array of alerts suggestions, but otherwise adding to the object you mentioned would look like this:

    alerts[3]={"app":"goodbyeworld","message":"cya"};
    

    But since you shouldn't use literal numbers as names quote everything and go with

    alerts['3']={"app":"goodbyeworld","message":"cya"};
    

    or you can make it an array of objects.

    Accessing it looks like

    alerts['1'].app
    => "helloworld"
    

提交回复
热议问题