简体   繁体   English

Sort() 正在改变我的原始数组。 有不同的解决方法吗?

[英]Sort() is mutating my original array. Is there a different workaround?

I've searched other threads and I couldn't find a working solution to my problem.我搜索了其他线程,但找不到解决问题的有效方法。 I tried slice() for the array but no luck.我为数组尝试了 slice() 但没有运气。 I've got a array, first I create a copy of the original array and after a few second I overwrite one value [0][1]=555.我有一个数组,首先我创建原始数组的副本,几秒钟后我覆盖一个值 [0][1]=555。 And this affects my copy/original array, the value 1000 gets overwritten with 555. Any ideas on how to overcome this?这会影响我的副本/原始数组,值 1000 会被 555 覆盖。关于如何克服这个问题的任何想法? I need to compare the two tables after the value gets changed.值更改后,我需要比较这两个表。 Here is my javascript code:这是我的 javascript 代码:

let intialstationtablevalues = [
["station name 1",75,100,100,100,100,100,100,0,100,100,100,100,1],
["station name 2",200,220,100,100,100,100,100,2,100,100,100,100,2],
["station name 3",300,110,100,100,100,100,100,0,100,100,100,760,3],
["station name 4",400,100,109,100,100,100,100,3,100,100,100,100,4],
["station name 5",500,100,100,100,100,100,100,0,100,100,100,100,5],
["station name 6",600,130,134,100,100,100,100,2,100,100,100,340,6],
["station name 7",700,100,100,100,100,100,100,0,100,100,100,100,7],
["station name 8",800,200,340,100,100,100,100,5,100,100,100,10,8],
["station name 9",900,100,100,100,100,100,100,0,100,100,100,6,9],
["station name 10",1000,100,900,100,100,100,100,2,100,100,100,100,10],
];
let old = []
let copy = []


setInterval(read,2000)
setTimeout(changevalue,10000)

function changevalue(){
  console.log("value changed")
  intialstationtablevalues[0][1]=555;
}


function read(){
        if(copy==""){
            copy = intialstationtablevalues.slice()
      }
        sortnow = intialstationtablevalues.sort(function(a,b){
            return b[1]-a[1];
        })

        console.log("sortnow",sortnow)

        if(old==""){
            copy.forEach(function(item,index){
                old.push(item)
            })
            old.sort(function(a,b){
                return b[1]-a[1];
            })
        }

        console.log("old",old)
}

sortedArray = [...arrayYouWantToSort].sort() Spreading the array you want to copy into a new array and then sort that array. sortedArray = [...arrayYouWantToSort].sort()将要复制的数组传播到一个新数组中,然后对该数组进行排序。 Fun way to do it!有趣的方式来做到这一点!

sortedArray = [...arrayYouWantToSort];
sortedArray[0][1] = whatever // whatever you wanna change
sortedArray.sort()

The main point is that spreading the arrayYouWantToSort into a new array literal creates a new array in memory.要点是将arrayYouWantToSort到一个新的数组字面量中会在memory 中创建一个新数组。 So arrayYouWantToSort and sortedArray are now total different physical parts of memory, but they have the same 'values'.因此, arrayYouWantToSortsortedArray现在是 memory 的完全不同的物理部分,但它们具有相同的“值”。 So you can change values in one without affecting the other.因此,您可以更改其中一个值而不影响另一个值。

So you first create the copied array, and then mutate the new array.因此,您首先创建复制的数组,然后对新数组进行变异。

I think.slice() is working fine here, try to replace the if statment with我认为.slice() 在这里工作正常,尝试将 if 语句替换为

if(copy.length ===0)

to check the empty array检查空数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM