我有一个像下面这样的对象
const tableData = [
{
"Location": "London",
"Status": "Unknown"
},
{
"Location": "Delhi",
"Status": "Reachable"
},
{
"Location": "Berlin",
"Status": "Unknown"
},
{
"Location": "Tokyo",
"Status": "Busy"
},
]
现在我想创建一个二维数组来以某种方式保存这些信息。以下是我的代码:
const statusOrder = {"Reachable": 0, "Busy": 1, "Unknown": 2}
let statusOrderInfo = Array(Object.keys(statusOrder).length).fill([]);
for(let i=0; i< tableData.length; i++) {
const status = tableData[i]["Status"].trim()
const statusIndex = statusOrder[status]
statusOrderInfo[statusIndex].push(tableData[i])
}
console.log(statusOrderInfo)
正如您所看到的,我希望将
tableData
对象的每个条目放置在2D数组的特定索引中。因此,包含Status
为Reachable
的条目应该在索引0处,包含Status
为Busy
的条目应该在索引1处,以此类推。因此,最终输出应如下所示:
[
[
{
"Location":"Delhi",
"Status":"Reachable"
}
],
[
{
"Location":"Tokyo",
"Status":"Busy"
}
],
[
{
"Location":"London",
"Status":"Unknown"
},
{
"Location":"Berlin",
"Status":"Unknown"
}
]
]
但是,尽管我定位了正确的索引,但在运行上述代码时,我得到了错误的输出。我的做法有什么问题?