Appending to an object

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

    [Javascript] After a bit of jiggery pokery, this worked for me:

     let dateEvents = (
                {
                    'Count': 2,
                    'Items': [
                        {
                            'LastPostedDateTime': {
                                "S": "10/16/2019 11:04:59"
                            }
                        },
                        {
                            'LastPostedDateTime': {
                                "S": "10/30/2019 21:41:39"
                            }
                        }
                    ],
                }
            );
            console.log('dateEvents', dateEvents);
    

    The problem I needed to solve was that I might have any number of events and they would all have the same name: LastPostedDateTime all that is different is the date and time.

    0 讨论(0)
  • 2020-11-27 11:05

    jQuery $.extend(obj1, obj2) would merge 2 objects for you, but you should really be using an array.

    var alertsObj = {
        1: {app:'helloworld','message'},
        2: {app:'helloagain',message:'another message'}
    };
    
    var alertArr = [
        {app:'helloworld','message'},
        {app:'helloagain',message:'another message'}
    ];
    
    var newAlert = {app:'new',message:'message'};
    
    $.extend(alertsObj, newAlert);
    alertArr.push(newAlert);
    
    0 讨论(0)
  • 2020-11-27 11:06

    How about storing the alerts as records in an array instead of properties of a single object ?

    var alerts = [ 
        {num : 1, app:'helloworld',message:'message'},
        {num : 2, app:'helloagain',message:'another message'} 
    ]
    

    And then to add one, just use push:

    alerts.push({num : 3, app:'helloagain_again',message:'yet another message'});
    
    0 讨论(0)
  • 2020-11-27 11:15

    Now with ES6 we have a very powerful spread operator (...Object) which can make this job very easy. It can be done as follows:

    let alerts = { 
       1: { app: 'helloworld', message: 'message' },
       2: { app: 'helloagain', message: 'another message' }
    } 
    
    //now suppose you want to add another key called alertNo. with value 2 in the alerts object. 
    
    alerts = {
       ...alerts,
       alertNo: 2
     }
    

    Thats it. It will add the key you want. Hope this helps!!

    0 讨论(0)
  • 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"
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题