附加到对象

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)

  • 此答案希望您知道要添加的警报的 ID。好吧,如果您已经知道这一点,那么您可以简单地遵循原始对象格式并执行:`alerts[3] = { app: 'hello3', message: 'message 3' }`。有了这个,您可以通过 id 访问消息:`alerts[2] => { app: 'helloworld', message: 'message' }`。获取长度就是:`Object.keys(alerts).length`(使用数组更容易*) (2认同)

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)

  • 这实际上是正确的答案,因为其他人指的是将对象转换为数组,但这样您就将它保留为对象.在作者的情况下,可能他最好使用数组,但这将是如何附加到对象的答案. (7认同)
  • 这就是标题问题的答案。 (3认同)
  • 是的,我同意..有问题的是,没有一个对象数组,而只是一个对象,所以这是一个完美的答案! (2认同)

Har*_*wal 8

现在有了 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)

就是这样。它将添加您想要的密钥。希望这可以帮助!!


dla*_*lin 7

您真的应该使用一系列警报建议,否则添加到您提到的对象将如下所示:

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)


M.C*_*M.C 7

与其他指出的答案一样,您可能会发现使用数组更容易.

如果不:

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


Pet*_*ley 5

你有能力把最外层的结构改成数组吗?所以它看起来像这样

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)

然后你有一个内置的从零开始的索引,用于枚举错误。


inz*_*nzo 5

您可以按如下方式使用传播语法。

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)