Get random item from Firebase

后端 未结 4 805
伪装坚强ぢ
伪装坚强ぢ 2020-12-01 11:18

I searched for it, but all the answers are pretty old so maybe there is a better way. I\'m trying to get a random item from a Firebase DB which looks like this:

4条回答
  •  萌比男神i
    2020-12-01 12:09

    First you have retrieve all the userid from the firebase.

    Then declare an array that stores all the userid in it. Below shows on how to store the userId.

    var arr = new Array();
    // or var arr = [];
    arr.push('user001');
    arr.push('user002');
    

    Next you have to select randomly from one of the userId.

    var item = arr[Math.floor(Math.random()*arr.length)];
    // item is randomly picked userId
    

    I'm sorry because i didn't completely show you on how the implementation works as I don't use javascript much.

    Basically this is how the logic works. I hope it helps you :D

提交回复
热议问题