3

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

1 Answer 1

2

You can use join method of Array like following:

this.textoFormat += this.texto.join('\n');
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Harun, Is there a need of +=? I think we can assign it directly.
Yes, indeed.textoFormat is initialized like this: public textoFormat: string = 'Fórmula \n'; And the output contains Formula \n in the beginning

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.