我有一個最多[32] [32]條目的2d數組。 我想從這樣的轉換:將多維數組轉換爲對象
[
null,
null,
null,
null,
null,
null,
[null, null, null, null, null, null, null, null, null, null, "player1"],
[null, null, null, null, null, "player2"]
]
到
{
"6": {"10":"player1"},
"7": {"5":"player2"}
}
因此,這將是我的數組:
var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
現在我試圖用這樣的:
var obj = {}
obj = Object.assign({},gameField);
console.log(JSON.stringify(obj));
但它只是工作的外陣,內陣並沒有受到影響:
{
"6": [null, null, null, null, null, null, null, null, null, null, "player1"],
"7": [null, null, null, null, null, "player2"]
}
什麼是正確地做到這一點最簡單的辦法?
你能解釋你想達到什麼嗎? – Rajesh
這似乎是一個非常糟糕的結構,在*和*之後。動態對象密鑰大多難以使用。你爲什麼不使用像''[player:'player1',numberOfX:10},{player:'player2',numberOfX:5}]'這樣更乾淨的東西。 – str
這個結構在現實中有點複雜。不僅僅是玩家。我想要的是簡單地將2d數組轉換爲使用數組索引作爲關鍵字的對象。這樣我可以擺脫數組的所有空值。 – Forivin