[英]How to retrieve data from 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
最简单的方法是什么?
谢谢!
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);
假设您要从Firebase检索数据,则将其存储在所谓的DataSnapshot中 。 该类具有方便的forEach()
方法,以允许对其子级进行迭代:
snapshot.forEach(function(child) {
data.push({
name: child.key(),
end: child.val().width,
start: child.val().height
});
})
我从Pamblam的答案中选择了start
和end
的逻辑,因为我不知道这是如何工作的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.