Reputation: 597
I'm new in Cloud FireStore and have been searching for many days without results, for a simple solution on how can I sum some fields value of an array coming from Observable Angular Firestore. I have a list of rows and I want sum and have the column total, here is part of my code:
constructor(private afs: AngularFirestore, private router: Router, private route: ActivatedRoute) {
this.negocio = this.route.snapshot.queryParams['negocioId']
this.movtosCollectionRef = this.afs.collection('movtos', ref => ref.where('empresa', '==', this.negocio));
this.movtos$ = this.movtosCollectionRef.snapshotChanges().map(actions => {
return actions.map(action => {
const data = action.payload.doc.data() as Movtos;
const id = action.payload.doc.id;
return { id, ...data };
});
});
}
In my template I make the browse :
<tbody>
<tr *ngFor="let item of movtos$ | async" [class.completed]="item.completed">
<td>{{ item.indate | date: 'dd/MM/yyyy - HH:mm:ss'}}</td>
<td>{{ item.concepto }} </td>
<td>{{ item.tipomov }} </td>
<td>{{ item.inuser }} </td>
<td>{{ item.importe | currency:'USD':true:'3.2-2' }}</td>
<td>
<a class="btn btn-info" (click)="editaEmpresa(item.id)" tooltip="Edita Movimiento" placement="top"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span></a>
</td>
</tr>
</tbody>
And I want to get the sum of item.importe field. Does any body know how to do this? Best regards, Caribesoft
Upvotes: 0
Views: 1506
Reputation: 19622
You can use map
on the Observable to return the sum. eg
getSum(): Observable<number> {
return this.observable
.map(arr => arr.reduce((a, b) => a + b.value, 0));
}
Upvotes: 1