Increment within the subscribe

Asked

Viewed 11 times

0

I have this method that gets the list date, what works, after a query in the database.

private preencherPlanosSaudes() {
    this.planoSaudeService.buscarTodos().subscribe(
      (data : any[]) => {
        let ps   = new EnumModel();
        ps.key   = data[0].id;
        ps.texto = data[0].nome;
        this.planosSaudes.push(ps);
      }
    );
  }

But inside the subscribe, I need an accountant to look like this:

private preencherPlanosSaudes() {
    let contador = 0;
    this.planoSaudeService.buscarTodos().subscribe(
      (data : any[]) => {
        let ps   = new EnumModel();
        ps.key   = data[contador].id;
        ps.texto = data[0contador].nome;
        this.planosSaudes.push(ps);
       contador ++;
      }
    );
  }

But it doesn’t work. What I’m doing wrong ?

  • how do you know it doesn’t work?

1 answer

0

It worked that way:

private preencherPlanosSaudes() {
    this.planoSaudeService.buscarTodos().subscribe(
      (data : any[]) => {
        data.forEach(
          d => {
            let ps   = new EnumModel();
            ps.key   = d.id;
            ps.texto = d.nome;
            this.planosSaudes.push(ps);
          }
        );
      }
    );

Browser other questions tagged

You are not signed in. Login or sign up in order to post.