gpt4 book ai didi

javascript - 如何仅从 firebase 中提取节点/值而不是键?

转载 作者:行者123 更新时间:2023-11-30 07:38:15 26 4
gpt4 key购买 nike

如何使用 javascript 仅从 firebase 中提取节点而不提取 key ?换句话说,我只想要来自下面的 firebase 的键值对的值,这意味着我不想要下面的唯一键,只想要下面的东西。

Firebase Data Structure

目前,我的代码是..

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();

这会在控制台中产生以下内容

[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"},...

但我只想拥有字典,例如

[{"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"}...]

最佳答案

如果我没理解错的话,您想要获取 quakes 下的所有子对象。

这里通常有两种方法:

  1. 获取父节点的值并循环子节点
  2. 监控子节点添加/更新/删除到父节点

您的方法与第 1 条相符,所以我会先回答第 1 条。我还将给出方法 #2 的示例,当您的数据集发生变化时,它会更有效。

迭代 Firebase ref 的子元素

在您的 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);

});

forEach 函数是同步的,所以我们可以简单地等待循环完成,然后创建交叉过滤器。

监控 Firebase ref 的 child

在那种情况下,最好的构造是:

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);
});

使用此代码结构,您可以主动监听添加和删除的地震。您仍然需要保留所有地震的数组,然后在 child_addedchild_changedchild_removed 中对其进行修改。

他们如何比较

当您第一次运行代码时,监视子项将产生与 on('value', 构造相同的数据。但是当稍后添加/删除子项时 on(' value', 将再次接收所有地震,而 on('child_added',on('child_removed', 将仅针对相关地震调用.

关于javascript - 如何仅从 firebase 中提取节点/值而不是键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24992867/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com