Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"... :(

like image 878
Gabriel Jaques Avatar asked Aug 01 '26 23:08

Gabriel Jaques


1 Answers

You can use join method of Array like following:

this.textoFormat += this.texto.join('\n');
like image 176
Harun Yilmaz Avatar answered Aug 03 '26 14:08

Harun Yilmaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!