[英]How to retrieve data from Firebase?
I'm having a little trouble retrieving data from firebase in a specific format, though it seems it should be simple. 我从Firebase以特定格式检索数据时遇到了一些麻烦,尽管看起来应该很简单。
The database endpoint containers this data in this format: 数据库端点以以下格式存储此数据:
{
"A": {
"width": 120,
"height": 130
},
"B": {
"width": 350,
"height": 240
},
"C": {
"width": 680,
"height": 160
}
All I want to do is concat the letters with their respective width and height, then push all of the groups into an array and set that equal to 'data' so I can call 'data' anywhere in the program to use it, like so: 我要做的就是将字母和它们各自的宽度和高度连接起来,然后将所有组推入数组并将其设置为等于“数据”,这样我就可以在程序中的任何地方调用“数据”来使用它,就像这样:
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
What's the simplest way to do this? 最简单的方法是什么?
Thanks! 谢谢!
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);
Given that you're retrieving data from Firebase, you'll get it in a so-called DataSnapshot . 假设您要从Firebase检索数据,则将其存储在所谓的DataSnapshot中 。 That class has a convenient
forEach()
method on it, to allow iterating over its children: 该类具有方便的
forEach()
方法,以允许对其子级进行迭代:
snapshot.forEach(function(child) {
data.push({
name: child.key(),
end: child.val().width,
start: child.val().height
});
})
I took the logic for start
and end
from Pamblam's answer, because I don't understand how that is meant to work. 我从Pamblam的答案中选择了
start
和end
的逻辑,因为我不知道这是如何工作的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.