对对象数组进行排序时出错 无法将对象“[对象数组]”的只读属性“2” 分配给只读
2022-08-30 04:55:27
我有对象数组,其中对象看起来像这样(值更改):
{
stats: {
hp: 2,
mp: 0,
defence: 4,
agility: 11,
speed: 6,
strength: 31
}
}
我想按速度按降序对它们进行排序:
array.sort((a, b) => {
return b.stats.speed - a.stats.speed
})
但是,我得到了这个错误,我无法真正破译发生了什么:
类型错误: 无法将对象“[对象数组]”的只读属性“2”
我错过了什么?
编辑:redux存储中的对象数组:
const enemyDefaultState = [
{
name: 'European Boy1',
stats: {
hp: 2,
mp: 0,
defence: 4,
agility: 11,
speed: 6,
strength: 31
}
},
{
name: 'European Boy2',
stats: {
hp: 2,
mp: 0,
defence: 4,
agility: 4,
speed: 2,
strength: 31
}
},
{
name: 'European Boy3',
stats: {
hp: 2,
mp: 0,
defence: 4,
agility: 7,
speed: 7,
strength: 31
}
},
]
我导入数组并将其分配给变量:
let enemies = getState().enemy;
if (enemies) {
//sort by speed stat
enemies.sort((a, b) => {
return b.stats.speed - a.stats.speed
})
}