I have a list observable of objects that I am trying to iterate over to compare a list object property to a local variable.
I can loop over the objects in the list in a template but can't see how to in the component logic.
How can I do this outside of the template?
This is the closest I have gotten to a working iterator so far.
constructor(af: AngularFire) {
this.competitors = af.database.list('/competitors');
}
this.competitors.forEach(competitor) {
console.log(competitor);
}
I'm sorry if this is a question is poorly constructed, I am still quite new to Angular2 and Firebase.
You're working with Observables and not plain collections here. This means that you have to use the Observable primitives and simple looping over this.competitors
will not work.
This this:
this.competitors.subscribe(competitor => console.log(competitor));
And by the way, there is a naming convention to name Observables with a $ at the end, so it would look the following way:
this.competitors$ = af.database.list('/competitors');
and:
this.competitors$.subscribe(competitor => console.log(competitor));
This will log the entire array of competitors. If you want to iterate over the elements, do something like the following:
this.competitors.subscribe(
competitors => {
competitors.map(competitor =>
console.log(competitor)
)
});