简体   繁体   中英

How to convert the content of a child array to a parent array

Im Using Angular 8 and Typescript

I have an array that displays email adresses. The user can input them by typing, or by uploading a CSV containing emails.

The problem is now that the csv parser loads its results as array into the array.

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.

this is the array:

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

this is the parser 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

 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'] ?

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;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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