附加到对象的

我有一个对象,其中包含警报和一些关于它们的信息:

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

除此之外,我还有一个变量,它表示有多少警报,alertNo。我的问题是,当我添加一个新的警报时,有没有一种方法可以将警报追加到 alerts对象上?

502841 次浏览

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

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'});

您是否有能力将最外层的结构更改为一个数组

var alerts = [{"app":"helloworld","message":null},{"app":"helloagain","message":"another message"}];

所以当你需要添加一个的时候,你可以把它放到数组中

alerts.push( {"app":"goodbyeworld","message":"cya"} );

然后,您有一个内置的从零开始的索引,用于说明如何枚举错误。

您真的应该使用警报建议数组,但是否则添加到您提到的对象将如下所示:

alerts[3]={"app":"goodbyeworld","message":"cya"};

但是,因为你不应该使用文字数字作为名称引用一切和去

alerts['3']={"app":"goodbyeworld","message":"cya"};

或者你可以把它变成一个对象数组。

看起来像是

alerts['1'].app
=> "helloworld"

试试这个:

alerts.splice(0,0,{"app":"goodbyeworld","message":"cya"});

工作得很好,它会把它添加到数组的开始。

JQuery$.extend(obj1, obj2)将为您合并两个对象,但您实际上应该使用一个数组。

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);

正如所指出的其他答案一样,您可能会发现使用数组更容易。

否则:

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"}
}

试试看:

Https://jsbin.com/yogimo/edit?js,console

你可以用 Object.sign ()做到这一点。有时候您需要一个数组,但是当使用期望单个 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"}
}

编辑: 为了解决关于获取下一个键的注释,您可以使用 Key ()函数获取一个键数组——参见 Vadi 关于递增键的示例的答案。类似地,您可以使用 Value ()获得所有值,使用 Object.entry ()获得键值对。

var alerts = {
1: {app:'helloworld',message:'message'},
2: {app:'helloagain',message:'another message'}
}
console.log(Object.keys(alerts))
// Output
Array [ "1", "2" ]

作为替代,在 ES6中可以使用扩展语法。

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);

您可以按照以下方式使用扩展语法。

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


alerts = {...alerts, 3: {app: 'hey there', message: 'another message'} }

现在有了 ES6,我们有了一个非常强大的扩展运算符(... 对象) ,它可以使这项工作非常容易。可以这样做:

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

我很抱歉,但我不能评论你的答案已经由于我的声誉!所以,如果你想修改你的对象的结构,像 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,所有不同的是日期和时间。