Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 重新分配参数_Javascript_Google Apps Script - Fatal编程技术网

Javascript 重新分配参数

Javascript 重新分配参数,javascript,google-apps-script,Javascript,Google Apps Script,我正试图从电子邮件中删除显示名称,例如 Stevetosteve@steve.com function test1() { var testemail = ["Steve<steve@steve.com>","displayname<display@steve.com>"]; var debug = stripEmail(testemail); var debug9 = ""; } function stripEmail(e

我正试图从电子邮件中删除显示名称,例如

Steve
to
steve@steve.com

    function test1() {
      var testemail = ["Steve<steve@steve.com>","displayname<display@steve.com>"];
  var debug = stripEmail(testemail);

      var debug9 = "";
    }

function stripEmail(email) {
  //Give me an email with a display name and I will strip out the display name
  //"<Steve Gon> stevegon@google.com"
  if (typeof email === 'string') {
    var arr = [email];
  } else {
    var arr = email;
  }

  for (i=0; i<arr.length; i++) {
    if (arr[i].search("<")>-1) {//If there is no less than, then it doesn't have a display name
      var part1 = arr[i].split("<");
      if (part1.length == 2) {
        arr[i] = part1[1].replace(">","");
        arr[i] = arr[i].replace("<","");
        arr[i] = arr[i].replace(" ","");
      } 
    } 
  }
  return arr;
}
函数test1(){
var testemail=[“Steve”,“displayname”];
var debug=stripEmail(testemail);
var=9=“”;
}
功能条带电子邮件(电子邮件){
//给我一封带有显示名的电子邮件,我会去掉显示名
//" stevegon@google.com"
如果(电子邮件类型=='string'){
var arr=[电子邮件];
}否则{
var arr=电子邮件;
}
对于(i=0;i

一旦我跨过函数,testemail就会改变。
如果要避免更改原始数组,请避免将一个数组设置为另一个数组。不要重复使用
email
变量,而是创建一个新的输出变量。在下面的代码中,电子邮件被放入名为
result
的新数组中

stripeEmail
函数同时处理字符串和数组,并始终返回与源数组不同的数组,保持原始数组不变

代码可以更改为:

function test1() {
  var testemail = ["Steve<steve@steve.com>","displayname<display@steve.com>"];
  Logger.log('testemail: ' + testemail)

  var debug = stripEmail(testemail);

  Logger.log('debug: ' + debug)
  Logger.log('testemail: ' + testemail)
}

function stripEmail(email) {
  var arr,i,part1,result;

  result = [];

  //Give me an email with a display name and I will strip out the display name
  //"<Steve Gon> stevegon@google.com"
  if (typeof email === 'string') {
    email = email.split(",");
  }

  for (i=0; i<email.length; i++) {
    if (email[i].search("<")>-1) {//If there is no less than, then it doesn't have a display name
      part1 = email[i].split("<");

      if (part1.length == 2) {
        result[i] = part1[1].replace(">","");
        result[i] = result[i].replace("<","");
        result[i] = result[i].replace(" ","");
      } 
    } 
  }

  return result;
}
函数test1(){
var testemail=[“Steve”,“displayname”];
Logger.log('testemail:'+testemail)
var debug=stripEmail(testemail);
Logger.log('debug:'+debug)
Logger.log('testemail:'+testemail)
}
功能条带电子邮件(电子邮件){
var arr,i,第1部分,结果;
结果=[];
//给我一封带有显示名的电子邮件,我会去掉显示名
//" stevegon@google.com"
如果(电子邮件类型=='string'){
email=email.split(“,”);
}
对于(i=0;i-1){//如果不小于,则它没有显示名称
第1部分=电子邮件[i]。拆分(“,”);

结果[i]=结果[i]。替换(“当您将数组传递给函数时,传递的是对数组的引用。没有复制。非常感谢您的快速响应……我想我已经不再使用W3schools了。这里没有任何解释:W3schools一直是一个非常糟糕和不可信的资源。请改为尝试MDN。@SteveGon在下对此进行了解释。”“对象是通过引用传递的”,尽管措辞非常糟糕。例如,如果您想通过值来测试调用,可以尝试像
var debug=stripeemail(testemail.concat())
var debug=stripeemail(testemail.slice())