For循环在第一次迭代后结束

时间:2019-02-01 04:46:32

标签: javascript for-loop google-apps-script

我正在尝试检查要使用GAS分配给每个工作的人。该函数针对包含以下两个数组的数组运行:负责客户的人员和他的客户列表。

为数组中的第一个客户端运行时,代码工作正常,它添加了成员​​和所有内容,因此我知道它是有效的。问题是它只能运行一次,因此如果客户端是“ PR”,它将添加“ lucasfogolin”作为成员,但是如果是CLC,它将不进行检查。

var clients = [{actor:'lucasfogolin',clients:'PR,CLC,Trívia,Smart,MC,TTWO'},
               {actor:'alfredorocha',clients:'FDV,IAB,IMDiva'}
              ]

我的排序功能如下:

function sortActors(notification) {

  //When a card is moved to the "Sorting list"
  var card = new Notification(notification).movedCard('Sorting');

  //Gets the cards name
  var cardName = card.name();

  for (var i = 0; i < clients.length; i+=1) {

    //Creates an array from splitting the clients
    var arrayClients = clients[i].clients.split(',');

    for (var j = 0; j < arrayClients.length; j+=1) {

      //Creates a REGEX to run against the card's name, not sensitive
      var regex = new RegExp(arrayClients[j],'gi');

      //Checks if the name of the client is in the name of the card
      if(cardName.match(regex)[0] == arrayClients[j]) {

        //Function that adds the actor to the card
        addMembers(card,clients[i].actor)
      }
    }
  }
  return false;
}

addMembers函数

function addMembers(card,members) {

  //Makes an array from the members cited (if more than one is to be added)
  var array = members.split(',');

  //Runs a loop to add all members
  for (var i = 0; i < array.length; i+=1) {
    card.addMember(new Member({username: array[i]}));
  }
}

2 个答案:

答案 0 :(得分:0)

更改

中的代码
    if(cardName.match(regex)[0] == arrayClients[j])

对此

    if(cardName.match(regex) != null && cardName.match(regex)[0] == arrayClients[j])

答案 1 :(得分:0)

在这部分

  //Checks if the name of the client is in the name of the card
  if(cardName.match(regex)[0] == arrayClients[j]) {

您正在检查正则表达式字符串匹配的元素0,如果您有匹配项就可以,但是当您没有匹配项时,cardName.match(regex)会返回null,但不会可以访问0值的元素null。那可能会造成问题。