JavaScript array is appending not, replacing element (NodeJS)

Hi I was trying to replace an item in an array, but instead it appends it to the array or something I have no idea what’s happening.

It’s meant to take in dice rolls from a array, take those input values and replace them with actual dice rolls and then replace the previous location with the new JSON that has the dice rolls and sum etc so that I can use them later.

function run_dice(dice){
  try{
    var valuesofdice = dice.value;
    var num;
    var size;
    var drop;
    var dcount = 0;
    var d1;
    var d2;
    for(i=0;i<(valuesofdice.length);i++){
      if(valuesofdice[i] === "d"){
        dcount+=1;
        if(dcount>2){
          return("error");
        }
      }
      if((valuesofdice[i] === "d") && dcount === 1){
        d1 = i;
        var num1 = (valuesofdice.substring(0,i));
        if (num1 == ""){
          num = 1;
        }else{
          num = Number(num1);
        }
      }else if((valuesofdice[i] === "d")&& dcount ===2){
        d2 = i;
        var num1 = (valuesofdice.substring(d1+1,d2));
        if(num1 === ""){
          return("error");
        }else{
          size = Number(num1);
        }
        var num2 = (valuesofdice.substring(d2+1));
        if (num2 == ""){
          return("error");
        }else{
          drop = Number(num2);
        }
      } 
      if((dcount === 1)&&(i == ((valuesofdice.length)-1))){
        var num1 = (valuesofdice.substring(d1+1,(valuesofdice.length)));
        if(num1 === ""){
          return("error");
        }else{
          size = Number(num1);
        }
      }
    }
    if(num == undefined && size == undefined && drop == undefined){
      return("error");
    }else if(drop == undefined){
      drop = 0;
    }
    if(drop>num){
      return("error");
    }
    if (num>1000 || size>1000000){
      return("error");
    }
    var arrayofrolls = [];
    var roll_sum = 0;
    for(i = 0;i<num;i++){
      var temp1 = 1 //LCG();
      arrayofrolls.push(((temp1)%size)+1);
    }
    arrayofrolls = quicksort(arrayofrolls);
    var drop_array = arrayofrolls.slice(0,drop);
    var sum_array = arrayofrolls.slice(drop,(arrayofrolls.length));
    for(i = 0;i<sum_array.length;i++){
      x = (sum_array[i]);
      roll_sum = roll_sum +x;
    }
    return({type:"dice",value:roll_sum,rolls:sum_array,drop:drop_array});
  }catch(e){
    return("error");
  }
}
var xz = [
  { type: 'dice', value: '2d6d1' },
  { type: '+' },
  { type: 'dice', value: '2d6' },
  { type: '*' },
  { type: 'dice', value: '8d6' }
];
for(i=0;i<(xz.length);i++){
  if((xz[i]).type === "dice"){
    console.log(i);
    var temp = (run_dice(xz[i]));
    //console.log(temp);
    xz[i] = temp;
  }
}
console.log(xz);