# My Array does not execute for or foreach

**URL:** <https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028>\
**Category:** ionic-v3\
**Created:** [October 19, 2018, 3:17pm UTC](https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028 "2018-10-19T15:17:45Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![easychurch](https://avatars.discourse-cdn.com/v4/letter/e/e495f1/32.png) [@easychurch](https://forum.ionicframework.com/u/easychurch)\
**Post date:** [October 19, 2018, 3:17pm UTC](https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028/1 "2018-10-19T15:17:45Z")

</div>

Hello, can someone help me with the following question?

I have declared a global Array in my component:

```auto
visitantes: Array<User>;

```

Then I populate this Array when I click on a button:

```auto
 finalizarVisitantes() {
    this.visitantes = this._userService.getVisitantesPorArray(this.org, this.batismo.membros);
    console.log('Batizandos', this.visitantes);
    console.log('Batizandos', this.visitantes.length);

    this.visitantes.forEach(item => {
      console.log('Item', item);
    });
}

```

On my page, the rendered items usually appear:

```auto
<ion-item *ngFor="let bt of visitantes">
        <ion-avatar slot="start">
            <img [src]="bt.photoURL">
          </ion-avatar>
      <ion-label>
        <h3>{{bt.displayName}}</h3>
        <p>
          {{bt.email}}
        </p>
      </ion-label>
    </ion-item>

```

But in my code I can not traverse the Array and in the console it gives different values.  
The first console shows all visitors.  
But in the second it gives zero lenght.  
And do not even enter my forEach.

![erro](https://global.discourse-cdn.com/ionicframework/original/3X/d/9/d9e49e48302350eecdc2b4ea0d74bfef6a54ed85.jpeg)

Anyone have any idea what might be happening?

---

<div class="post-metadata">

**Author:** ![Jacktoolsnet](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/jacktoolsnet/32/107318_2.png) [@Jacktoolsnet](https://forum.ionicframework.com/u/Jacktoolsnet)\
**Post date:** [October 19, 2018, 3:36pm UTC](https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028/2 "2018-10-19T15:36:33Z")

</div>

I woud say the function \_userService.getVisitantesPorArray() woud take same time to execute.  
But js wont wait and executes the other lines before the result comes back from the function.

Try to use async and await.

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [October 19, 2018, 7:44pm UTC](https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028/3 "2018-10-19T19:44:46Z")

</div>

Please post the definition of `getVisitantesPorArray`.

---

<div class="post-metadata">

**Author:** ![easychurch](https://avatars.discourse-cdn.com/v4/letter/e/e495f1/32.png) [@easychurch](https://forum.ionicframework.com/u/easychurch)\
**Post date:** [October 19, 2018, 7:46pm UTC](https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028/4 "2018-10-19T19:46:53Z")

</div>

```auto
    getVisitantesPorArray(org: Org, visitantes: string[]): Array<User> {
      const arr: Array<User> = [];
      for (const x of visitantes) {
        this.getVisitante(org, x).forEach(item => {
          if (item.email === x) { arr.push(item); }
        });
      }
      return arr;
    }

```

---

<div class="post-metadata">

**Author:** ![easychurch](https://avatars.discourse-cdn.com/v4/letter/e/e495f1/32.png) [@easychurch](https://forum.ionicframework.com/u/easychurch)\
**Post date:** [October 19, 2018, 7:48pm UTC](https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028/5 "2018-10-19T19:48:03Z")

</div>

I tried, but it did not work. Would you have an example to show?

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [October 19, 2018, 7:49pm UTC](https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028/6 "2018-10-19T19:49:09Z")

</div>

OK, there’s nothing overtly asynchronous in there, so what I would do is to make sure that `visitantes` is initialized at the point of declaration:

```auto
visitantes: Array<User> = [];

```

---

<div class="post-metadata">

**Author:** ![easychurch](https://avatars.discourse-cdn.com/v4/letter/e/e495f1/32.png) [@easychurch](https://forum.ionicframework.com/u/easychurch)\
**Post date:** [October 19, 2018, 7:51pm UTC](https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028/7 "2018-10-19T19:51:48Z")

</div>

Yes, I already did this test. is an array of string and is passing the data correctly.

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [October 19, 2018, 7:54pm UTC](https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028/8 "2018-10-19T19:54:39Z")

</div>

> [@easychurch](#):
>
> is an array of string

I’m not talking about the argument in the provider, but rather the array in the component. You say

> [@easychurch](#):
>
> I populate this Array when I click on a button

That implies to me that until that button is clicked, the `visitantes` property in the component that you are trying to iterate across with `forEach` is `undefined`. That will throw an error during rendering that will mysteriously break virtually everything in your page. Always initialize all component properties referred to in templates.

---

<div class="post-metadata">

**Author:** ![easychurch](https://avatars.discourse-cdn.com/v4/letter/e/e495f1/32.png) [@easychurch](https://forum.ionicframework.com/u/easychurch)\
**Post date:** [October 19, 2018, 8:00pm UTC](https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028/9 "2018-10-19T20:00:42Z")

</div>

I do not think I understand.  
This array is within the method and is temporary.  
It is being started in the method, filled with getVisitant and returned at the end.

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [October 19, 2018, 8:04pm UTC](https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028/10 "2018-10-19T20:04:00Z")

</div>

I wouldn’t use the term “global” here:

> [@easychurch](#):
>
> I have declared a **global** Array in my component:
> 
> ```auto
> visitantes: Array<User>;
> 
> ```

…but what I think you mean is a controller property, aka:

```auto
export class Page {
  visitantes: Array<User>;
  ...
}

```

That’s the only way you could be using it in the associated template with `forEach`. “Temporary” or lexically-scoped variables are invisible to templates.

This is the `visitantes` that I’m suggesting initializing:

```auto
export class Page {
  visitantes: User[] = [];
  ...
}

```

---

<div class="post-metadata">

**Author:** ![easychurch](https://avatars.discourse-cdn.com/v4/letter/e/e495f1/32.png) [@easychurch](https://forum.ionicframework.com/u/easychurch)\
**Post date:** [October 19, 2018, 8:14pm UTC](https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028/11 "2018-10-19T20:14:26Z")

</div>

Tb did this and did not solve.  
That’s strange. In a line of code it renders the values and soon in the next line the same variable renders another without no other action.

---

<div class="post-metadata">

**Author:** ![Jacktoolsnet](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/jacktoolsnet/32/107318_2.png) [@Jacktoolsnet](https://forum.ionicframework.com/u/Jacktoolsnet)\
**Post date:** [October 19, 2018, 8:31pm UTC](https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028/12 "2018-10-19T20:31:51Z")

</div>

> [@easychurch](#):
>
> this.getVisitante(org, x)

What does this function?

---

<div class="post-metadata">

**Author:** ![easychurch](https://avatars.discourse-cdn.com/v4/letter/e/e495f1/32.png) [@easychurch](https://forum.ionicframework.com/u/easychurch)\
**Post date:** [October 19, 2018, 8:36pm UTC](https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028/13 "2018-10-19T20:36:30Z")

</div>

returns an object visitante: `<User>`

---

<div class="post-metadata">

**Author:** ![Jacktoolsnet](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/jacktoolsnet/32/107318_2.png) [@Jacktoolsnet](https://forum.ionicframework.com/u/Jacktoolsnet)\
**Post date:** [October 19, 2018, 8:38pm UTC](https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028/14 "2018-10-19T20:38:25Z")

</div>

And it is not asynchron too?

Ok and have you checked that  
item.email === x  
isn’t always false?

---

<div class="post-metadata">

**Author:** ![easychurch](https://avatars.discourse-cdn.com/v4/letter/e/e495f1/32.png) [@easychurch](https://forum.ionicframework.com/u/easychurch)\
**Post date:** [October 19, 2018, 9:08pm UTC](https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028/15 "2018-10-19T21:08:01Z")

</div>

I changed now to:

```auto
 getVisitantesPorArray(org: Org, visitantes: string[]): User[] {
      const arr: User[] = [];
      for (const x of visitantes) {
        this.getVisitante(org, x).pipe(map(item => arr.push(item)));
      }
      return arr;
    }

```

And continues with the same problem.

---

<div class="post-metadata">

**Author:** ![Jacktoolsnet](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/jacktoolsnet/32/107318_2.png) [@Jacktoolsnet](https://forum.ionicframework.com/u/Jacktoolsnet)\
**Post date:** [October 19, 2018, 9:17pm UTC](https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028/16 "2018-10-19T21:17:44Z")

</div>

sorry no more ideas on my side

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [October 19, 2018, 9:50pm UTC](https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028/17 "2018-10-19T21:50:10Z")

</div>

> [@easychurch](#):
>
> pipe(map(item =\> arr.push(item)))

`<img src="giant-flashing-siren.gif">`

You can’t pretend that asynchronous things are synchronous. `getVisitantesPorArray` cannot return `Array<User>`. I was under the impression that `forEach` was `Array.forEach`.

Now the entire discussion is radically different, and I don’t speak enough Portuguese to know what `getVisitantesPorArray` needs to be called, but it probably shouldn’t be that. See [this post](https://forum.ionicframework.com/t/access-the-selected-object-of-ngfor-into-ts/144746/11) for some background.

```auto
needsProperName(org: Org, visitantes: string[]): Observable<User[]> {
  return from(visitantes).pipe(
    mergeMap(vn => this.getVisitante(org, vn)),
    toArray()
  );
}

```

---

<div class="post-metadata">

**Author:** ![easychurch](https://avatars.discourse-cdn.com/v4/letter/e/e495f1/32.png) [@easychurch](https://forum.ionicframework.com/u/easychurch)\
**Post date:** [October 22, 2018, 2:52pm UTC](https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028/18 "2018-10-22T14:52:10Z")

</div>

Thanks for the personal help.  
I found a library called RxFire.  
I used the combinedLatest design with it, and I was able to do the merge of objects.  
So, I no longer have to generate an Array.

Your suggestion @rapropos has opened this window.

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [October 22, 2018, 5:53pm UTC](https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028/19 "2018-10-22T17:53:05Z")

</div>

Glad you found something that worked for you, and thanks for reporting back so that others in your situation might benefit.

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 9:12pm UTC](https://forum.ionicframework.com/t/my-array-does-not-execute-for-or-foreach/145028/20 "2021-07-19T21:12:34Z")

</div>

Closed on July 19, 2021, 9:12pm UTC


