Proper way to store values array-like in Firebase

前端 未结 3 1811
花落未央
花落未央 2020-12-08 22:54

I have a name of the song, with ratings I am saving for it. And it looks like pic bellow, as I am using push() to store each new rating.

3条回答
  •  时光说笑
    2020-12-08 23:38

    After get the values from database firebase, you can storege it in another array using forEach(), like so:

     const arrayPlayers = []
            this.db.database.ref('player').once('value')
               .then((snapshot)=> {               
                    snapshot.forEach((childSnapShot:any)=>{
                        arrayPlayers.push(childSnapShot.val())
                    })
                    //Here your array will be withut the node, like so:
                    //[3,5,1,...]
                    //Now sort array with JS
                    const sortPlayers = arrayPlayers.sort((a:any, b:any)=> b.score - a.score)
                    console.log(sortPlayers);
    
                })
    

提交回复
热议问题