简体   繁体   English

如何将子数组的内容转换为父数组

[英]How to convert the content of a child array to a parent array

Im Using Angular 8 and Typescript我正在使用 Angular 8 和 Typescript

I have an array that displays email adresses.我有一个显示 email 地址的数组。 The user can input them by typing, or by uploading a CSV containing emails.用户可以通过键入或上传包含电子邮件的 CSV 来输入它们。

The problem is now that the csv parser loads its results as array into the array.现在的问题是 csv 解析器将其结果作为数组加载到数组中。

Console log of array item:数组项的控制台日志:

(When user types): email: "email@test.de"
(when user uses csv upload): email: Array [ "email@test.de" ]

I need this "child" arrays content to be converted to the parent array as plain string, because the user can trigger a send invite function, which causes the mail to be sent to {"email@test.de"} in the case of csv upload.我需要将这个“子”arrays 内容作为纯字符串转换为父数组,因为用户可以触发发送邀请 function,这会导致邮件发送到 {“email@test.de”} csv 上传。

this is the array:这是数组:

<mat-chip
        *ngFor="let email of emails"
        {{ email }}
      </mat-chip>

this is the parser function:这是解析器 function:

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 is the invite function这是邀请 function

 onInvite() {
    const requests = [];
    this.emails.forEach((email) => {
      const invitaionRequest = this.inviteService
        .invite(email, this.data.role, this.data.challengeId)
        .pipe(
          catchError((err) => {
            console.error(err);
            this.snackbar.open(err.error.message, 'Ok', {
              duration: 5000,
            });
            return of(err);
          })
        );
      requests.push(invitaionRequest);
    });

So you want to convert the csv upload from [['email@email.com'], ['email2@email.com']] to ['email@email.com','email2@email.com'] ?所以你想将 csv 上传从[['email@email.com'], ['email2@email.com']]转换为['email@email.com','email2@email.com']

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

  this.csvService
    .parse(files[0], { header: false })
    .pipe()
    .subscribe((result: Array<any>) => {
      // assuming there is only a single email per array in your results array
      let emails = result.map(i => i[0]);
      this.emails = emails;
  }
}

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

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