我有一个可以容纳警报和有关警报的信息的对象:
var alerts = {
1: { app: 'helloworld', message: 'message' },
2: { app: 'helloagain', message: 'another message' }
}
除此之外,我还有一个变量,表示有多少警报alertNo
。我的问题是,当我添加新警报时,是否可以将警报附加到alerts
对象上?
我有一个可以容纳警报和有关警报的信息的对象:
var alerts = {
1: { app: 'helloworld', message: 'message' },
2: { app: 'helloagain', message: 'another message' }
}
除此之外,我还有一个变量,表示有多少警报alertNo
。我的问题是,当我添加新警报时,是否可以将警报附加到alerts
对象上?
Answers:
如何将警报作为记录而不是单个对象的属性存储在数组中?
var alerts = [
{num : 1, app:'helloworld',message:'message'},
{num : 2, app:'helloagain',message:'another message'}
]
然后添加一个,只需使用push
:
alerts.push({num : 3, app:'helloagain_again',message:'yet another message'});
alerts[3] = { app: 'hello3', message: 'message 3' }
。这样,您可以通过id访问消息:alerts[2] => { app: 'helloworld', message: 'message' }
。获取长度则刚:Object.keys(alerts).length
(该位是与阵列更容易)
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);
您可以使用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"}
}
编辑:要解决有关获取下一个键的注释,您可以使用Object.keys()函数获取键的数组-有关增加键的示例,请参见Vadi的答案。同样,您可以使用Object.values()获得所有值,并使用Object.entries()获得键值对。
var alerts = {
1: {app:'helloworld',message:'message'},
2: {app:'helloagain',message:'another message'}
}
console.log(Object.keys(alerts))
// Output
Array [ "1", "2" ]
像指出的其他答案一样,您可能会发现使用数组更容易。
如果不:
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"}
}
试试吧:
while(alerts[size]) size++;
并且可能会重命名size
为newId
。
现在有了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
}
而已。它将添加您想要的密钥。希望这可以帮助!!
使用ES6更轻松:
let exampleObj = {
arg1: {
subArg1: 1,
subArg2: 2,
},
arg2: {
subArg1: 1,
subArg2: 2,
}
};
exampleObj.arg3 = {
subArg1: 1,
subArg2: 2,
};
console.log(exampleObj);
{
arg1: {subArg1: 1, subArg2: 2}
arg2: {subArg1: 1, subArg2: 2}
arg3: {subArg1: 1, subArg2: 2}
}
作为替代方案,在ES6中,可以使用传播语法。${Object.keys(alerts).length + 1}
返回下一个id
警报。
let alerts = {
1: {app:'helloworld',message:'message'},
2: {app:'helloagain',message:'another message'}
};
alerts = {
...alerts,
[`${Object.keys(alerts).length + 1}`]:
{
app: `helloagain${Object.keys(alerts).length + 1}`,message: 'next message'
}
};
console.log(alerts);
抱歉,由于我的声誉,我已经无法评论您的答案!...因此,如果您想修改对象的结构,则必须像Thane Plummer所说的那样,但是如果您不在乎哪里,可以使用一些技巧放置物品:如果您没有指定插入的编号,它将被插入第一个位置。
如果您想将一个Json对象传递给mongoDB函数调用,并在收到的条件内插入一个新键,这将是很棒的。在这种情况下,我将插入代码myUid以及代码中变量中的一些信息:
// From backend or anywhere
let myUid = { _id: 'userid128344'};
// ..
// ..
let myrequest = { _id: '5d8c94a9f629620ea54ccaea'};
const answer = findWithUid( myrequest).exec();
// ..
// ..
function findWithUid( conditions) {
const cond_uid = Object.assign({uid: myUid}, conditions);
// the object cond_uid now is:
// {uid: 'userid128344', _id: '5d8c94a9f629620ea54ccaea'}
// so you can pass the new object Json completly with the new key
return myModel.find(cond_uid).exec();
}
[Javascript]玩了几千个扑克之后,这对我有用:
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);
我需要解决的问题是,我可能有许多事件,并且它们都具有相同的名称:LastPostedDateTime唯一不同的是日期和时间。