2 回答

TA貢獻1854條經(jīng)驗 獲得超8個贊
假設(shè)您有 5 名玩家的限制。在這種情況下,這個:
const players = new Array(matchInfo.playersAllowedInGame).fill().map(p => {
return {
stats: statsWithKeys,
dropdownOpen: false,
id: Math.random().toString(36)
}
})
沒有像您期望的那樣創(chuàng)建 5 個 'statsWithKeys',而是 5 個對相同 'statsWithKeys' 的引用。
解決此問題的最佳方法是直接在對象本身上使用擴展運算符:
const players = new Array(matchInfo.playersAllowedInGame).fill().map(p => {
return {
stats: { ...statsWithKeys },
dropdownOpen: false,
id: Math.random().toString(36)
}
});
添加回答
舉報