2

背景:

我是一个大家庭的一员,为了在圣诞节为每个人节省一些钱,我们做了一个秘密圣诞老人来送礼。我正在编写这个脚本,以便可以通过电子表格管理这一切,因为我们的过程可能有些混乱。规则是:

  1. 每个“圣诞老人”都有两个名字,他们必须为其购买礼物。
  2. 这两个名字不能相同。
  3. 夫妻不能给对方或他们的孩子礼物。孩子不能给他们的兄弟姐妹或父母送礼物。

这是一个包含一些示例数据的表格: 样本数据

问题

我相信我的问题是由于以下代码而发生的:

//Remove disallowedNames from currentAvailableNames
for (j=0; j<disallowed.length; j++){
  var disallowedName = disallowed[j];
  currentAvailableNames.splice(currentAvailableNames.indexOf(disallowed[j]), 1);
}

出于某种原因,不允许的名称也从 availableNames 数组中删除,我不知道为什么。我能够“修复”它的唯一方法是在选择收件人后添加以下代码:

//Add Disallowed Names back to Available Names Array
for (k=0; k<disallowed.length; k++){
  var disallowedName = disallowed[k];
  if (disallowedName.length >0) {
    availableNames.push(disallowedName);
  }
}

原始代码

function giftAssignments() {
  //Get Settings
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var settings = ss.getSheetByName("Settings");
  var resultsSheet = ss.getSheetByName("Results");
  var numOfAssignments = settings.getRange("B2").getValue();
  var minPrice = settings.getRange("B3").getValue();
  var maxPrice = settings.getRange("B4").getValue();
  var firstName = settings.getRange("B5").getValue();
  var santasLastRow = settings.getLastRow();
  var santasLastCol = settings.getLastColumn();
  var santasTotal = santasLastRow - firstName + 1;
  var santasAsRange = settings.getRange(firstName,1,(santasLastRow - firstName + 1), santasLastCol).getValues();

  //Create Santas Array (santas)
  var santas = []
  for (var i=0; i<santasAsRange.length; i++) {
    var name = santasAsRange[i][0];
    var email = santasAsRange[i][1];
    var disallowedAsString = santasAsRange[i][2];
    disallowedAsString = disallowedAsString.replace(", ",",");
    var disallowed =  disallowedAsString.split(",");
    disallowed.push(name);
    var santa = [];
    santa[0] = name;
    santa[1] = email;
    santa[2] = disallowed;
    santas.push(santa);
  }
  //Create Array of Names (availableNames)
  var availableNames = [];
  for (i=0; i<santas.length; i++) {
    var aName = santas[i][0];
    availableNames.push(aName);
  }
  //Assign Recipients
  var results = assignRecip(santas, availableNames);
  Logger.log("RESULTS = " + results);
}


function assignRecip(santas, names) {
  var availableNames = names;
  for (i=0; i<santas.length; i++) {
    var currentAvailableNames = availableNames;
    var name = santas[i][0];
    var disallowed = santas[i][2];
    Logger.log("Santa = " + name);
    Logger.log("availableNames = " + availableNames);

    //Remove disallowedNames from currentAvailableNames
    for (j=0; j<disallowed.length; j++){
      var disallowedName = disallowed[j];
      currentAvailableNames.splice(currentAvailableNames.indexOf(disallowed[j]), 1);
    }
    Logger.log("currentAvailableNames = " + currentAvailableNames);

    //Pick Random Ricipient from currentAvailableNames
    var recipient = currentAvailableNames[Math.floor(Math.random() * currentAvailableNames.length)];
    Logger.log("Recipient = " + recipient); 

    //Add Recipient to Santa Array
    santas[i].push(recipient);

    //Add Disallowed Names back to Available Names Array
    for (k=0; k<disallowed.length; k++){
      var disallowedName = disallowed[k];
      if (disallowedName.length >0) {
        availableNames.push(disallowedName);
      }
    }

    //Add Recipient to Disallowed Names Array
    santas[i][2].push(recipient);

    //Remove Recipient from Available Names Array
    availableNames.splice(availableNames.indexOf(recipient),1);
    Logger.log("availableNames = " + availableNames);
    Logger.log(" ");
  }
  return santas;
}
4

1 回答 1

4

它们是对同一个数组的引用。此代码不会复制 Array 本身。它复制对数组的引用。

var currentAvailableNames = availableNames;

您可以使用.slice().

var currentAvailableNames = availableNames.slice();

现在你有两个独立的数组,所以直接修改currentAvailableNames不会影响availableNames.

请注意,这是一个浅克隆。如果它是对象数组或数组,则对嵌套对象的修改仍然可以从两个数组中看到。

于 2012-10-30T19:30:19.330 回答