Gabriel Jaques
Gabriel Jaques

Reputation: 27

Concatenating string array

I have an array of strings, with the following values;

public texto: string[] = []; =>

0: "Tacrolimus em adultos"
1: "Sugestão - p07"
2: "Anti-aging - B (oral)"
3: "Tomar duas vezes ao dia."

I need to concatenate each element of the array by jumping a line into a variable. This variable will be used to insert value into a textarea field. I am using logic for this, but without success.

this.texto.forEach(e => {
      const str = `${e} \n`;
      this.textoFormat.concat(str);
    });

Typescript =>

public formula: any;
  public medicamento: string[] = [];
  public sugestao: string[] = [];
  public composto: string[] = [];
  public texto: string[] = [];
  public textoFormat: string = 'Fórmula \n';

ngOnInit() {
    this.formula = this._formulaService.formulaSugeridaTexto;

    this.formula.medicamento.forEach(medicamento => {
      this.medicamento.push(medicamento.nome);
    });

    this.formula.sugestao.forEach(sugestao => {
      this.sugestao = sugestao.nome;
    });

    this.texto.push(this.formula.nome);
    this.texto.push(this.sugestao.toString());
    this.texto.push(this.medicamento.toString());
    this.texto.push(this.formula.modo_uso);

    this.texto.forEach(e => {
      const str = `${e} \n`;
      this.textoFormat.concat(str);
    });
  }

HTML =>

<ion-textarea rows="15" cols="20" [value]="textoFormat"> </ion-textarea>

I hope the output is:

Fórmula
    Tacrolimus em adultos
    Sugestão - p07
    Anti-aging - B (oral)
    Tomar duas vezes ao dia.

but nothing is being shown, only the string set by default "Formula"... :(

Upvotes: 2

Views: 2330

Answers (1)

Harun Yilmaz
Harun Yilmaz

Reputation: 8558

You can use join method of Array like following:

this.textoFormat += this.texto.join('\n');

Upvotes: 2

Related Questions