2017-05-13 49 views
0

我正在開發離子2應用程序。 我有這樣的數據在火力 image獲取多個firebase節點中的數據總和

我需要的是去throgh的考試節點,並得到總的所有痕跡。在firebase文檔中有 有這樣的例子。

var query = firebase.database().ref("users").orderByKey(); 
query.once("value") 
    .then(function(snapshot) { 
    snapshot.forEach(function(childSnapshot) { 
     // key will be "ada" the first time and "alan" the second time 
     var key = childSnapshot.key; 
     // childData will be the actual contents of the child 
     var childData = childSnapshot.val(); 
    }); 
}); 

但我不知道如何使用這個爲我的情況。 任何人都可以請幫助我?

謝謝

回答

0

你可以做到這一點相當的總和容易lodash

>_ npm install --save lodash 

import { forEach,sum} from 'lodash'; 

... 

let marks:[]; 
forEach(this.exam,value => { 
    marks.push(value.mark); 
}); 
console.log(sum(marks)); 

Sum

+0

thnx爲想法。無限期需要使用和檢查.Thnx – LVW23

0

你的JSON結構是錯誤的,重新通過這個你可以循環,實現如下

exams : [ { name: '50' , marks :50 }, 
      { name: 'exam1' , marks :60 } 

     ] 

並計算所有標記

+0

感謝您的快速回復。這裏是我的代碼添加新的檢查和marks.but但如果我要做你喜歡你的答案如何添加標記separtely.It只會覆蓋所有其他數據。 'addexams(userid){let exams = {examid:this.examno,marks:this.marks}返回新的Promise((reslove)=> {this.userProfile.child(userid).set({exams}); reslove( true);}); } @Aravind @aravind – LVW23

+0

我沒有得到你。精心製作 – Aravind

相關問題