Copying array inside a function

I am working on a function where keeping a copy of some original array is important. In the course of the function are the following lines:

var orgcid = [...cid];
cid = [].concat(cid.reverse());

cid refers to an inputted array. Later in the code, there are operations performed on cid, but not on orgcid. However, both end up changing. To create orgcid as an independent copy, I have tried the spread operation (as in above), concatenating the original to an empty array, and the slice method. All of them fail to create an independent copy. Any insight into the problem would be appreciated.

Below is the whole function if you would like a look:


function checkCashRegister(price, cash, cid) {
  //prep and data
  var youGet = cash - price;
  var change = [];
  var output = { status: null, change: [] }
  const monKey = { 'PENNY': 0.01, 'NICKEL': 0.05, 'DIME': 0.1, 'QUARTER': 0.25, 'ONE': 1, 'FIVE': 5, 'TEN': 10, 'TWENTY': 20, 'ONE HUNDRED': 100};
  
  //check to see if there's a priliminary sum
  if (cid.reduce((acc, curr) => acc += curr[1], 0) < youGet) {
    output.status = "INSUFFICIENT_FUNDS"
    output.change = [];
    return output;
  };
  
  //prep for distribution
  var orgcid = [...cid];
  cid = [].concat(cid.reverse());

  //iterate and distribute cash top to bottom per value
  for (let i = 0; i < cid.length && youGet > 0; i++) {
    if (youGet > monKey[cid[i][0]] && cid[i][1] != 0) {
      if (youGet >= cid[i][1]) {
        change.push([].concat(cid[i]));
        youGet = Math.round((youGet - cid[i][1])* 100) / 100;
        cid[i][1] = 0;
      }
      else {
        let takeNow = monKey[cid[i][0]] * parseInt(youGet / monKey[cid[i][0]]);
        change.push([].concat([cid[i][0], takeNow]));
        youGet = Math.round((youGet - takeNow) * 100) / 100
        cid[i][1] = Math.round((cid[i][1] - takeNow) * 100) / 100;
      }
    }
  }
  
  //customer got exact change?
  if (youGet != 0) {
    output.status = "INSUFFICIENT_FUNDS"
    output.change = [];
    return output;
  };

  //is there money left in register?
  if (cid.reduce((acc, curr) => acc += curr[1], 0) == 0) {
    output.status = "CLOSED";
    output.change = orgcid;
    return output;
  };
  
  output.status = "OPEN";
  output.change = change;
  return output;
}

I think it is because it’s a nested array and the copy is shallow.

Edit: not really sure what the “best” way to make a deep copy is but you can try this.

const duplicateObject = JSON.parse(JSON.stringify( cid ));

that worked. time to try and understand it now. thanks!