Appending to an object

后端 未结 13 1492
心在旅途
心在旅途 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: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);
    

提交回复
热议问题