动态命名顶级对象键

时间:2019-09-24 23:14:27

标签: javascript

我正在尝试将套接字分配给特定用户的ID,并且似乎卡住了,例如,我有以下代码,

let user_list = {}

let socket = {}
socket[456] = "I am a socket"

user_list[123] = {...user_list, socket }
console.log(user_list)

我正在尝试获得以下结果:

{
  "123": {
    "456": "I am a socket"
  }
}

2 个答案:

答案 0 :(得分:2)

只需将散布运算符与socket对象一起使用:

let user_list = {}

let socket = {}
socket[456] = "I am a socket"

user_list[123] = {...user_list, ...socket}
console.log(user_list)

答案 1 :(得分:0)

您不必为其本身分配<table id="example1" class="table table-bordered table-responsive"> <thead> <tr> <th>Id</th> <th>Producto</th> <th>Precio</th> </tr> </thead> <tbody> <tr> <td>@Html.DisplayFor(model => model.Id)</td> <td>@Html.DisplayFor(model => model.Nombre)</td> <td>@Html.DisplayFor(model => model.Precio) </td> </tr> </table> ,请参见以下更改:

user_list