Appending to an object

后端 未结 13 1498
心在旅途
心在旅途 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:16

    Do you have the ability to change the outer-most structure to an array? So it would look like this

    var alerts = [{"app":"helloworld","message":null},{"app":"helloagain","message":"another message"}];
    

    So when you needed to add one, you can just push it onto the array

    alerts.push( {"app":"goodbyeworld","message":"cya"} );
    

    Then you have a built-in zero-based index for how the errors are enumerated.

提交回复
热议问题