【发布时间】:2014-02-24 08:37:43
【问题描述】:
如何在 .on 方法完成循环我的 firebase 中的所有项目并递增所有值后打印“obj”值?简而言之,我在这里要做的是,一旦循环完成,我想将结果(“obj”)打印到控制台。
在这里感谢任何帮助!谢谢。
var obj = {
"20120101" : {"neutral": 0, "positive": 1, "negative": 2},
"20120101" : {"neutral": 0, "positive": 1, "negative": 2}
} //above is just an example on the structure of the object
var fbase = new Firebase("https://<appname>.firebaseio.com/");
fbase.on('child_added', function(snapshot){
var item = snapshot.val(); //has the attributes of: date & classification
var date_str = item.date;
if (!obj[date_str]){
//initialise the counting;
obj[date_str] = { "negative": 0, "neutral": 0, "positive" : 0 };
}
obj[date_str][item.classification] += 1;
});
console.log(obj);
【问题讨论】:
标签: javascript node.js firebase