javascript - 如何从 Firebase 检索数据?

标签 javascript object firebase

我在以特定格式从 firebase 检索数据时遇到了一些麻烦,尽管看起来应该很简单。

数据库端点以这种格式包含此数据:

{
    "A": {
        "width": 120,
        "height": 130
    },
    "B": {
        "width": 350,
        "height": 240
    },
    "C": {
        "width": 680,
        "height": 160
}

我想做的就是用它们各自的宽度和高度连接字母,然后将所有组放入一个数组并将其设置为等于“数据”,这样我就可以在程序的任何地方调用“数据”来使用它,像这样:

    var data = [{
    "id" : "A",
    "width": 120,
    "height": 130
}, {
    "id" : "B",
    "width": 350,
    "height": 240
}, {
    "id" : "C",
    "width": 680,
    "height": 160
}];

   // somewhere else in the file
  // data[0].end === 150

最简单的方法是什么?

谢谢!

最佳答案

Here's a fiddle.

var junk = {
    "A": {
        "width": 120,
        "height": 130
    },
    "B": {
        "width": 350,
        "height": 240
    },
    "C": {
        "width": 680,
        "height": 160
        }
};
var data = [];
for (var property in junk) {
    if (junk.hasOwnProperty(property)) {
        data.push({
            name:property,
            end: junk[property].width,
            start:junk[property].height
        });
    }
}
console.log(data);

关于javascript - 如何从 Firebase 检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34292604/

相关文章:

javascript - 存储对地址簿应用程序的实例化对象的引用

java - Android firebase 应用发布多个值

ios - 从 firebase 获取链接后图像不显示

javascript - jquery - 将参数传递给函数

javascript - 如何递归调用/运行 jQuery 嵌套模板

javascript - 在jquery中动态获取输入标签onkeypress的值

javascript - jQuery 在 Rails 应用程序中没有反应

Javascript 三元 if 语句,将其分配给变量并递增

c++ - 面向对象的问题

node.js - Firebase Cloud Firestore 触发器 context.auth 始终为 null