Reputation: 1077
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.
Upvotes: 3
Views: 4245
Reputation: 10187
Iterate with next
in subscribe:
this.transactions.subscribe({
next(competitors) {
competitors.forEach(competitor => {
console.log(competitor);
});
},
error(err) { console.log('errors already caught... will not run'); }
})
Upvotes: 0
Reputation: 18904
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)
)
});
Upvotes: 7
Reputation: 1100
this.competitors.forEach(competitor) {
var competitorValue = competitor.val();
console.log(competitorValue);
competitorValue.forEach(sub){
console.log(sub.val());
}
}
This will let you parse through all the branches.
Upvotes: 0