javascript 对对象数组进行排序时出错无法分配给对象“[object Array]”的只读属性“2”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53420055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:11:33  来源:igfitidea点击:

Error while sorting array of objects Cannot assign to read only property '2' of object '[object Array]'

javascriptsorting

提问by MazMat

I'm having array of objects where object looks like this (values change):

我有对象数组,其中对象看起来像这样(值更改):

   {
     stats: {
        hp: 2,
        mp: 0,
        defence: 4,
        agility: 11,
        speed: 6,
        strength: 31
     }
   }

I want to sort them in descending order by speed doing:

我想按速度按降序对它们进行排序:

  array.sort((a, b) => {
            return b.stats.speed - a.stats.speed
        })

However I'm getting this error and I can't really decipher whats going on:

但是我收到了这个错误,我无法真正破译发生了什么:

TypeError: Cannot assign to read only property '2' of object '[object Array]'

类型错误:无法分配给对象“[object Array]”的只读属性“2”

What am I missing?

我错过了什么?

Edit: Array of object in redux store:

编辑: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
    }
},

]

]

I import the array and assign it to the variable:

我导入数组并将其分配给变量:

 let enemies = getState().enemy;
        if (enemies) {
            //sort by speed stat
            enemies.sort((a, b) => {
                return b.stats.speed - a.stats.speed
            })
        }

回答by Patrick Roberts

Because the array is frozenin strict mode, you'll need to copy the array before sorting it:

因为数组在严格模式下冻结,你需要在排序之前复制数组:

array = array.slice().sort((a, b) => b.stats.speed - a.stats.speed)

回答by Nick Friedman

The reason as Patrick stated is because the array is frozen. So any method of copying the array will work such as the one he suggests.

帕特里克所说的原因是因为数组被冻结了。所以任何复制数组的方法都可以像他建议的那样工作。

array = array.slice().sort((a, b) => b.stats.speed - a.stats.speed)

array = array.slice().sort((a, b) => b.stats.speed - a.stats.speed)

I just want to add that the reasonthe array is frozen in your case is because your using the array as props from the redux store and props in React are immutable hence your not being able to mutate the array.

我只想补充一点,在您的情况下,数组被冻结的原因是因为您使用数组作为来自 redux 存储的道具,而 React 中的道具是不可变的,因此您无法改变数组。

回答by slash

The array is frozen to prevent mutation of the redux state. You use react cloneElement(): https://reactjs.org/docs/react-api.html#cloneelement

数组被冻结以防止 redux 状态的突变。你使用反应 cloneElement(): https://reactjs.org/docs/react-api.html#cloneelement

[...enemies].sort((a, b) => {
                return b.stats.speed - a.stats.speed
            })