从firebase快照中选择随机值 [英] Pick random value from firebase snapshot
问题描述
我使用firebase的foreach从这个url获取树中的每个孩子
目标,当页面从firebase中抓取一个随机项并显示
数据结构
grabbit(表名)
$
$ b
在页面加载时进入 http://gamerholic.firebase.com/grabbit/active
并抓住任何一个类别并将其返回。
脚本
var grabbitRef = new Firebase('https://gamerholic.firebaseIO.com/grabbit/active/') ;
grabbitRef.on('value',function(snapshot){
if(snapshot.val()=== null){
alert(invalid);
}其他{
//获取快照数据:
snapshot.forEach(函数(快照){
var name = snapshot.name();
alert(name );
});
}
});
当我有一个随机类别说电子,我得到一个新的快照,并返回任何电子产品中的随机物品
var grabbitRef = new Firebase('https://gamerholic.firebaseIO.com/grabbit/active/ +名);
grabbitRef.on('value',function(snapshot){
if(snapshot.val()=== null){
alert(invalid);
}其他{
//获取快照数据:
snapshot.forEach(function(snapshot){
var id = snapshot.name();
alert(id );
});
}
});
使用ID我现在可以获取该物品的详细信息
var grabbitRef = new Firebase('https://gamerholic.firebaseIO.com/grabbit/active/'+name+'/'+id);
无法从列表中获取随机项目在Firebase,不幸的是。您可以限制(1)抓取第一个项目,或者使用endAt()。limit(1)抓取最后一个项目。
如果使用forEach,你也可以抓住所有的项目,然后使用Math.random随机选择一个项目。例如:
var i = 0;
var rand = Math.floor(Math.random()* snapshot.numChildren());
snapshot.forEach(function(snapshot){
if(i == rand){
//选取随机物品,snapshot.val()。
}
i ++;
});
I'm using firebase's foreach to get each child in a tree from this url
Objective, when the page loads grab a random item from firebase and show it
data structure
grabbit (table name)
active (for active items for sale)
category (the category of the item ie womensClothes, MensShoes etc)
unique id of the item
On page load go into http://gamerholic.firebase.com/grabbit/active and grab any one of the categories and return it..
Script
var grabbitRef = new Firebase('https://gamerholic.firebaseIO.com/grabbit/active/');
grabbitRef.on('value', function(snapshot) {
if(snapshot.val() === null) {
alert("invalid");
} else {
// get snap shot data:
snapshot.forEach(function(snapshot) {
var name = snapshot.name();
alert(name);
});
}
});
After I have a random category say "electronics", I get a new snapshot and have it return any random item that's in electronics
var grabbitRef = new Firebase('https://gamerholic.firebaseIO.com/grabbit/active/'+name);
grabbitRef.on('value', function(snapshot) {
if(snapshot.val() === null) {
alert("invalid");
} else {
// get snap shot data:
snapshot.forEach(function(snapshot) {
var id = snapshot.name();
alert(id);
});
}
});
with the id I can now get the details of the item
var grabbitRef = new Firebase('https://gamerholic.firebaseIO.com/grabbit/active/'+name+'/'+id);
It's not possible to grab a random item from the list in Firebase, unfortunately. You can do limit(1) to grab the first item, or endAt().limit(1) to grab the last item.
If you're using forEach, you can also grab all the items like you are and then picking one at random, using Math.random. For example:
var i = 0;
var rand = Math.floor(Math.random() * snapshot.numChildren());
snapshot.forEach(function(snapshot) {
if (i == rand) {
// picked random item, snapshot.val().
}
i++;
});
这篇关于从firebase快照中选择随机值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!