2017-12-24 5 views
0

私はCloud FireStoreの新機能で、Observableから来る配列のいくつかのフィールド値をどのように集計することができるのか簡単な解決法が得られました。アングルファイヤーストア。私は、行のリストを持っていると私は合計をしたいと列の合計を持って、ここに私のコードの一部です:Observableのフィールド値をAngular 4 Firestoreアレイで計算する方法

私のテンプレートで
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 }; 
     }); 


    }); 
    } 

私は、ブラウズします:

 <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> 

をそして私が取得したいですitem.importeフィールドの合計。 どのように体はこれを行う方法を知っていますか? よろしくお願いします。 Caribesoft

答えて

0

Observableでmapを使用すると、合計を返すことができます。例:

getSum(): Observable<number> { 
    return this.observable 
     .map(arr => arr.reduce((a, b) => a + b.value, 0)); 
} 
+0

あなたの回答はありがたいですが、「this.observable」はどこにありますか?私はthis.movtos $で試しましたが、うまくいきません。 – CaribeSoft

+0

あなたは約束としての応答を得ていますか?次に、あなたは 'Observed.fromPromise()'を使うことができます。 –

+0

私はこれを試しています - > getSum():Observable { return this.movtos $ .map(arr => arr.reduce((a、b)=> a + b.importe、0)); }ですが、私は を取得しますTOTAL:[オブジェクトオブジェクト] – CaribeSoft

関連する問題