Ste*_*uso 141 javascript
我有一个对象,它包含警报和一些有关它们的信息:
var alerts = {
1: { app: 'helloworld', message: 'message' },
2: { app: 'helloagain', message: 'another message' }
}
Run Code Online (Sandbox Code Playgroud)
除此之外,我还有一个变量,说明有多少警报alertNo.我的问题是,当我去添加新警报时,有没有办法将警报附加到alerts对象上?
And*_*ech 223
如何将警报存储为数组中的记录而不是单个对象的属性?
var alerts = [
{num : 1, app:'helloworld',message:'message'},
{num : 2, app:'helloagain',message:'another message'}
]
Run Code Online (Sandbox Code Playgroud)
然后添加一个,只需使用push:
alerts.push({num : 3, app:'helloagain_again',message:'yet another message'});
Run Code Online (Sandbox Code Playgroud)
res*_*ode 59
jQuery $.extend(obj1, obj2)会为你合并2个对象,但你应该真正使用一个数组.
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);
Run Code Online (Sandbox Code Playgroud)
Tha*_*mer 31
您可以使用Object.assign()执行此操作.有时候你真的需要一个数组,但是在处理期望单个JSON对象的函数时 - 比如OData调用 - 我发现这个方法比创建一个数组只是为了解压缩它更简单.
var alerts = {
1: {app:'helloworld',message:'message'},
2: {app:'helloagain',message:'another message'}
}
alerts = Object.assign({3: {app:'helloagain_again',message:'yet another message'}}, alerts)
//Result:
console.log(alerts)
{
1: {app:'helloworld',message:'message'},
2: {app:'helloagain',message:'another message'}
3: {app: "helloagain_again",message: "yet another message"}
}
Run Code Online (Sandbox Code Playgroud)
现在有了 ES6,我们有了一个非常强大的扩展运算符 (...Object),它可以使这项工作变得非常容易。可以按如下方式完成:
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
}
Run Code Online (Sandbox Code Playgroud)
就是这样。它将添加您想要的密钥。希望这可以帮助!!
您真的应该使用一系列警报建议,否则添加到您提到的对象将如下所示:
alerts[3]={"app":"goodbyeworld","message":"cya"};
Run Code Online (Sandbox Code Playgroud)
但是由于您不应该使用文字数字作为名称引用所有内容并使用
alerts['3']={"app":"goodbyeworld","message":"cya"};
Run Code Online (Sandbox Code Playgroud)
或者您可以将其设为对象数组。
访问它看起来像
alerts['1'].app
=> "helloworld"
Run Code Online (Sandbox Code Playgroud)
与其他指出的答案一样,您可能会发现使用数组更容易.
如果不:
var alerts = {
1: {app:'helloworld',message:'message'},
2: {app:'helloagain',message:'another message'}
}
// Get the current size of the object
size = Object.keys(alerts).length
//add a new alert
alerts[size + 1] = {app:'Your new app', message:'your new message'}
//Result:
console.log(alerts)
{
1: {app:'helloworld',message:'message'},
2: {app:'helloagain',message:'another message'}
3: {app: "Another hello",message: "Another message"}
}
Run Code Online (Sandbox Code Playgroud)
试试吧:
https://jsbin.com/yogimo/edit?js,console
你有能力把最外层的结构改成数组吗?所以它看起来像这样
var alerts = [{"app":"helloworld","message":null},{"app":"helloagain","message":"another message"}];
Run Code Online (Sandbox Code Playgroud)
所以当你需要添加一个时,你可以把它推到阵列上
alerts.push( {"app":"goodbyeworld","message":"cya"} );
Run Code Online (Sandbox Code Playgroud)
然后你有一个内置的从零开始的索引,用于枚举错误。
您可以按如下方式使用传播语法。
var alerts = {
1: { app: 'helloworld', message: 'message' },
2: { app: 'helloagain', message: 'another message' }
}
alerts = {...alerts, 3: {app: 'hey there', message: 'another message'} }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
344644 次 |
| 最近记录: |