简体   繁体   English

如何从数组 object 中删除花括号和双引号

[英]How to remove curly braces and double quotes from array object

At this Project i use Angular 8 and typescript.在这个项目中,我使用 Angular 8 和 typescript。

I have an array that displays emails upon type input or upon csv upload.我有一个数组,可以在输入类型或 csv 上传时显示电子邮件。 once input of some emails is done, there is a button to send invitations to these mails.一旦输入了一些电子邮件,就会有一个按钮来向这些邮件发送邀请。

To load the emails into the array i use this code (this.emails is the identifier for the array which is set up as emails: string[] = []; and called via ngfor loop in the html)要将电子邮件加载到数组中,我使用此代码(this.emails 是设置为emails: string[] = [];并通过 html 中的 ngfor 循环调用)

 onFileInput($event: any): void {
    const files = $event.target.files as File;

    this.csvService
      .parse(files[0], { header: false })
      .pipe()
      .subscribe((result: Array<any>) => {
        this.emails = result;
      });
  }

This works fine as it adds the emails to the array.这很好用,因为它将电子邮件添加到数组中。 But when i use the invite function, it identifies the email in the array as {"email@test.de"} while it should actually be just email@test.de.但是当我使用邀请 function 时,它将数组中的 email 标识为 {"email@test.de"},而实际上它应该只是 email@test.de。

The wierd part is that the array actually just shows email@test.de.奇怪的是该数组实际上只显示 email@test.de。 And when one enters the email via input, the invite function properly identifies is as email@test.de当一个人通过输入进入email时,邀请function正确识别为email@test.de

So the problem seems to be that what gets loaded to the array with the code above is actually not a plain string所以问题似乎是用上面的代码加载到数组中的实际上不是纯字符串

the array logs a csv imported emails as email: Array [ "email@test.de" ] while it logs a type input mail as email: "email@test.de" (as it should be)该数组将 csv 导入的电子邮件记录为email: Array [ "email@test.de" ]而将类型输入邮件记录为email: "email@test.de"应该是:"email@test

So is there a way to modify the function to remove the curly braces and double quotes before it enters the array?那么有没有办法修改 function 在进入数组之前去掉大括号和双引号呢?

you can use regExpr to remove the {} and []您可以使用 regExpr 删除 {} 和 []

this.csvService
  .parse(files[0], { header: false })
  .pipe(
     map((emails:any[])=>
         emails.map((email:string)=>email.match(/([\[{]).+([\]}])/)?
                      email.match(/([\[{]).+([\]}])/)[0].slice(1,-1):
                      email
       )
     )
  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM