Chr*_*ris 1 javascript dictionary firebase
你如何只使用firebase从firebase而不是键中拉出节点?换句话说,我只想要来自下面的firebase的键值对的值,这意味着我不想要下面的唯一键,而只需要下面的内容.

目前,我的代码是......
function PullFirebase() {
new Firebase('https://myfirebase.firebaseIO.com/quakes').on('value', function (snapshot) {
var S = snapshot.val();
function printData(data) {
var f = eval(data);
console.log(data + "(" + f.length + ") = " + JSON.stringify(f).replace("[", "[\n\t").replace(/}\,/g, "},\n\t").replace("]", "\n]"));
}
printData(S);
});
}
PullFirebase();
Run Code Online (Sandbox Code Playgroud)
这会在控制台中生成以下内容
[object Object](undefined) = {"-JStYZoJ7PWK1gM4n1M6":{"FID":"quake.2013p618454","agency":"WEL(GNS_Primary)","depth":"24.5703","latitude":"-41.5396","longitude":"174.1242","magnitude":"1.7345","magnitudetype":"M","origin_geom":"POINT (174.12425 -41.539614)","origintime":"2013-08-17T19:52:50.074","phases":"17","publicid":"2013p618454","status":"automatic","type":"","updatetime":"2013-08-17T19:54:11.27"},
"-JStYZsd6j4Cm6GZtrrD":{"FID":"quake.2013p618440","agency":"WEL(GNS_Primary)","depth":"26.3281","latitude":"-38.8725","longitude":"175.9561","magnitude":"2.6901","magnitudetype":"M","origin_geom":"POINT (175.95611 -38.872468)","origintime":"2013-08-17T19:45:25.076","phases":"13","publicid":"2013p618440","status":"automatic","type":"","updatetime":"2013-08-17T19:48:15.374"},...
Run Code Online (Sandbox Code Playgroud)
但我想只有字典,比如
[{"FID":"quake.2013p618454","agency":"WEL(GNS_Primary)","depth":"24.5703","latitude":"-41.5396","longitude":"174.1242","magnitude":"1.7345","magnitudetype":"M","origin_geom":"POINT (174.12425 -41.539614)","origintime":"2013-08-17T19:52:50.074","phases":"17","publicid":"2013p618454","status":"automatic","type":"","updatetime":"2013-08-17T19:54:11.27"},{"FID":"quake.2013p597338","agency":"WEL(GNS_Primary)","depth":"5.0586","latitude":"-37.8523","longitude":"176.8801","magnitude":"2.2362","magnitudetype":"M","origin_geom":"POINT (176.88006 -37.852307)","origintime":"2013-08-10T00:21:54.989","phases":"17","publicid":"2013p597338","status":"automatic","type":"","updatetime":"2013-08-10T03:42:41.324"}...]
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,你想要获得所有子对象quakes.
你通常有两种方法:
你的方法与#1匹配,所以我先回答那个问题.我还将举例说明方法#2,当数据集发生变化时效率更高.
在on('value',处理程序中,您可以使用forEach以下命令跳过唯一ID :
new Firebase('https://myfirebase.firebaseIO.com/quakes').on('value', function (snapshot) {
var quakes = [];
snapshot.forEach(function(childSnapshot) {
quakes.push(childSnapshot.val());
});
var filter = new crossfilter(quakes);
});
Run Code Online (Sandbox Code Playgroud)
该forEach函数是同步的,因此我们可以简单地等待循环完成然后创建交叉过滤器.
在这种情况下,最好的结构是:
var quakes = new Firebase('https://myfirebase.firebaseIO.com/quakes');
var quakeCount = 0;
quakes.on('child_added', function (snapshot) {
var quake = snapshot.val();
quakeCount++;
console.log("quakeCount="+quakeCount+", FID="+quake.FID);
});
quakes.on('child_removed', function (old_snapshot) {
var quake = old_snapshot.val();
quakeCount--;
console.log("quakeCount="+quakeCount+", removed FID="+quake.FID);
});
Run Code Online (Sandbox Code Playgroud)
使用此代码构造,您可以主动侦听添加和删除的地震.你仍然必须保留所有的地震,然后您可以修改的数组child_added,child_changed和child_removed.
首次运行代码时,监视子项将导致与on('value',构造相同的数据.但是当孩子被添加/移除后,on('value',将再次收到所有地震,on('child_added',而且on('child_removed',只会被召唤到有问题的地震.
| 归档时间: |
|
| 查看次数: |
1463 次 |
| 最近记录: |