-1

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. 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. And this affects my copy/original array, the value 1000 gets overwritten with 555. Any ideas on how to overcome this? I need to compare the two tables after the value gets changed. Here is my javascript code:

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)
}
5
  • You make a copy with .slice() but then you proceed to sort the original array anyway. The .sort() method sorts the array for which it's invoked. Commented Nov 8, 2019 at 16:10
  • .slice is the correct solution but you've fallen into shallow vs deep copy confusion Commented Nov 8, 2019 at 16:12
  • stackoverflow.com/questions/122102/… Commented Nov 8, 2019 at 16:13
  • if(copy=="") - That's not how you should test for an empty array. It works but only because JavaScript has truthy/falsy values. Commented Nov 8, 2019 at 16:14
  • Aluan Haddad so is there a way of doing this right to get what I want ? Commented Nov 8, 2019 at 16:17

3 Answers 3

2

sortedArray = [...arrayYouWantToSort].sort() Spreading the array you want to copy into a new array and then sort that array. 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. So arrayYouWantToSort and sortedArray are now total different physical parts of memory, but they have the same 'values'. So you can change values in one without affecting the other.

So you first create the copied array, and then mutate the new array.

Sign up to request clarification or add additional context in comments.

9 Comments

Ah didn't read the whole thing sorry. Still just use the spread operator. Ill update my response
Well I think I've done it correctly if(copy.length===0){ copy = [...intialstationtablevalues] }
should do it. And then use the copy
it still throws the 555 value in the copy array or maybe I'm doing somethind wrong...;)
Hmm. The only way that could happen is either intialStationableValues has 555 set before you spread, or you set it after. What is the point of this function exactly?
|
1

As of 2023 .toSorted() can be used to return sorted array without mutating original array

Comments

-1

I think .slice() is working fine here, try to replace the if statment with

if(copy.length ===0)

to check the empty array

1 Comment

I'm still getting the same 555 result in the copy array

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.