0

I have a 2D Array containing values like such: [[A, B], [C, D], [E, F]]

Based on some parameters I loop through the Array and when I find a match, I want to apply this match to another variable without it having a reference to the original array, so that I can manipulate both freely.

Here is my code:

while (change > 0) {
    for (let i = 0; i < cashAvail.length; i++) {
        if (change / cashAvail[i][0] >= 1 && cashAvail[i][1] > 0) {
            cur = cashAvail[i].slice(0)
        }
    }
    change = 0;
}

ignore change = 0; that is for debugging purposes.

cashAvail is my 2D Array. Lets assume that after the loop, the final value for cur is [C, D]. In this scenario, if I check if cashAvail.includes(cur) it returns false, even though cur == [C, D] and cashAvail remained unchange such that cashAvail[1] == [C, D]

if I remove the .slice() in the code, cashAvail.includes(cur) returns true, but of course I cant alter either arrays now, without altering the other.

Am I having a brainfart moment here? why can I not get this to work.

0 Answers0