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

后端 未结 4 595
执笔经年
执笔经年 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

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

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

提交回复
热议问题