Angular 获取多个firebase节点中的数据总和
我正在开发一个离子2应用程序。 我在firebase中有这种数据 我需要的是通过考试节点获得所有分数的总和。 在firebase文档中有这样的示例Angular 获取多个firebase节点中的数据总和,angular,typescript,firebase-realtime-database,ionic2,Angular,Typescript,Firebase Realtime Database,Ionic2,我正在开发一个离子2应用程序。 我在firebase中有这种数据 我需要的是通过考试节点获得所有分数的总和。 在firebase文档中有这样的示例 var query = firebase.database().ref("users").orderByKey(); query.once("value") .then(function(snapshot) { snapshot.forEach(function(childSnapshot) { // key will be
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();
});
});
但我不知道如何在我的案件中使用这个。
谁能帮帮我吗
谢谢您的json结构错误,请重新配置以实现以下目标
exams : [ { name: '50' , marks :50 },
{ name: 'exam1' , marks :60 }
]
通过此操作,您可以循环并计算所有标记的总和您可以在
谢谢您的快速回复。这是我添加新的examno和分数的代码。但是,如果我喜欢你的答案,那么如何单独添加分数。它只会覆盖所有其他数据
Addexams(userid){let-exams={examid:this.examno,marks:this.marks}返回新的承诺((reslove)=>{this.userProfile.child(userid).set({exams});reslove(true);});}
@Aravind@aravindI没有理解你。为这个想法精心设计。肯定需要使用和检查。Thnx
>_ npm install --save lodash
import { forEach,sum} from 'lodash';
...
let marks:[];
forEach(this.exam,value => {
marks.push(value.mark);
});
console.log(sum(marks));