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

后端 未结 4 464
执笔经年
执笔经年 2020-12-09 07:05

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

   {
     stats: {
        hp: 2,
        mp: 0,
        defence: 4,
        agili         


        
4条回答
  •  醉梦人生
    2020-12-09 07:40

    To be clear, the issue is not purely that the array is frozen. Frozen arrays can be iterated over. As noted in ReactJS - sorting - TypeError: 0 is read only, the issue is that Array.sort sorts the array in-place, meaning it attempts to mutate the array. That's why you need to pass it a mutable copy of the array.

提交回复
热议问题