附加到对象 [英] Appending to an object

查看:55
本文介绍了附加到对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以容纳警报和有关警报的信息的对象:

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

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

除此之外,我还有一个变量,表示有多少警报,alertNo.我的问题是,当我要添加新警报时,是否可以将警报附加到alerts对象上?

In addition to this, I have a variable that says how many alerts there are, alertNo. My question is, when I go to add a new alert, is there a way to append the alert onto the alerts object?

推荐答案

如何将警报作为记录而不是单个对象的属性存储在数组中?

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'} 
]

然后添加一个,只需使用push:

And then to add one, just use push:

alerts.push({num : 3, app:'helloagain_again',message:'yet another message'});

这篇关于附加到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆