我遇到的问题如下:我正在尝试用另一个对象(称为消息)中的内容填充Javascript对象(称为Table)
我正在尝试通过将Table对象中的2个键与Messages对象中的2个键进行匹配来实现此目的。我想要匹配的键可以被描述为'row'和'cell'。
如果消息'row'和'cell'匹配,我将消息推入单元格。
信息 - 行是对象顶层的唯一键,称为“实验室”。 - 单元格是行中的唯一键,但Cell键可能在另一行中有重复。它们被称为“步骤”。
我的想法是,要唯一地标识一个单元格,可以将我要填充的表格单元格的Row + Cell键与消息的Row + Cell键进行比较。如果匹配,则填充表格单元格。
我正在尝试填充的对象看起来如下(在填充之前):
{
"570f5233c8640c8819dae6a2": {
"570f5215c8640c8819dae6a0": {
"_id": "570f5215c8640c8819dae6a0",
"time": 2,
"type": "Type 1",
"order": 0,
"messages": []
},
"570f927196fa91ab0a1d8793": {
"_id": "570f927196fa91ab0a1d8793",
"time": 0,
"type": "Type 2",
"order": 1,
"messages": []
}
},
"570f52caecbdcb2c1a7031f1": {
"570f5215c8640c8819dae6a0": {
"_id": "570f5215c8640c8819dae6a0",
"time": 2,
"type": "Type 1",
"order": 0,
"messages": []
},
"570f927196fa91ab0a1d8793": {
"_id": "570f927196fa91ab0a1d8793",
"time": 0,
"type": "Type 2",
"order": 1,
"messages": []
}
}
}
我用来填充上面对象的代码如下(我正在填充消息字段):
for (let msgKey in messages) {
if (messages.hasOwnProperty(msgKey)) {
for (let tableRowKey in table) {
if (table.hasOwnProperty(tableRowKey)) {
for (let cellRowKey in table[tableRowKey]) {
if (table[tableRowKey].hasOwnProperty(cellRowKey)) {
let msgRow = messages[msgKey].lab.toString();
let msgStep = messages[msgKey].step.toString();
if (tableRowKey === msgRow && cellRowKey === msgStep) {
table[tableRowKey][cellRowKey].messages.push(messages[msgKey]);
}
}
}
}
}
}
}
代码的输出如下(填充后):
{
"570f5233c8640c8819dae6a2": {
"570f5215c8640c8819dae6a0": {
"_id": "570f5215c8640c8819dae6a0",
"time": 2,
"type": "Type 1",
"order": 0,
"messages": [
{
"step": "570f5215c8640c8819dae6a0",
"time": 2,
"timeActual": 0.00010335648148148148,
"timeDifference": null,
"isComplete": true,
"lab": "570f5233c8640c8819dae6a2"
}
]
},
"570f927196fa91ab0a1d8793": {
"_id": "570f927196fa91ab0a1d8793",
"time": 0,
"type": "Type 2",
"order": 1,
"messages": [
{
"step": "570f927196fa91ab0a1d8793",
"time": 0,
"timeActual": 0.00007015046296296297,
"timeDifference": null,
"isComplete": true,
"lab": "570f52caecbdcb2c1a7031f1"
}
]
}
},
"570f52caecbdcb2c1a7031f1": {
"570f5215c8640c8819dae6a0": {
"_id": "570f5215c8640c8819dae6a0",
"time": 2,
"type": "Type 1",
"order": 0,
"messages": [
{
"step": "570f5215c8640c8819dae6a0",
"time": 2,
"timeActual": 0.00010335648148148148,
"timeDifference": null,
"isComplete": true,
"lab": "570f5233c8640c8819dae6a2"
}
]
},
"570f927196fa91ab0a1d8793": {
"_id": "570f927196fa91ab0a1d8793",
"time": 0,
"type": "Type 2",
"order": 1,
"messages": [
{
"step": "570f927196fa91ab0a1d8793",
"time": 0,
"timeActual": 0.00007015046296296297,
"timeDifference": null,
"isComplete": true,
"lab": "570f52caecbdcb2c1a7031f1"
}
]
}
}
}
如果匹配正确,当消息“lab”键和消息“step”键与父键匹配时,消息将填充在对象中。所以正确的对象看起来如下:
"570f5233c8640c8819dae6a2": {
"570f5215c8640c8819dae6a0": {
"_id": "570f5215c8640c8819dae6a0",
"time": 2,
"type": "Type 1",
"order": 0,
"messages": [
{
"step": "570f5215c8640c8819dae6a0",
"time": 2,
"timeActual": 0.00010335648148148148,
"timeDifference": null,
"isComplete": true,
"lab": "570f5233c8640c8819dae6a2"
}
]
}
但是,代码实际上是在父键不匹配的字段中填充消息,如上面包含的后填充代码所示。
这里到底出了什么问题,对我来说代码看起来是正确的,我是否遗漏了有关匹配Javascript密钥的内容?