![](/img/trans.png)
[英]How do I loop through two objects with multiple arrays inside to get the values I want?
[英]how do I loop through this database and get the child values?
所以第一個鍵是用戶uid取自auth,然后他/她提供的用戶名以及他們為每場比賽得分的內容也被采取..
我只是希望得到每個用戶的總分 - 例如Ray總分為45分,而Wood總分為44分,但在查看了文檔之后,我能夠做的只是一個用戶,我必須編寫每個用戶名和每條線的具體匹配得到的價值..現在想想如果他們是幾十個用戶會是怎樣的? 嗯很多線..
這是JSON
javascript代碼
var query = firebase.database().ref();
query.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var key = childSnapshot.key;
var Data1 = childSnapshot.child("Ray/Match1/Points").val();
var Data2 = childSnapshot.child("Ray/Match2/Points").val();
console.log(Data1 + Data2);
});
})
這將讓我顯示,雷總分,但不是伍德顯然我必須重復它並寫它..
那么我該如何解決這個問題呢?
只需將總節點添加到您的數據庫
|_Id
|_ $userId:
| |_ Ray
| | |_ Match1:24
| | |_ Match2:21
| |_ total:45
然后得到用戶的總數
var query = firebase.database().ref();
query.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var total = childSnapshot.child("total").val();
console.log(total);
});
})
您可以使用雲功能添加總節點
我看了你的問題,我認為我有你的解決方案,或者至少是你解決方案的途徑。 好的,首先我將解釋基本問題,然后我將嘗試為您提供一些泛型代碼(我將嘗試使用您使用的一些變量)。 我們走吧!
基本上我看到的是兩步......
第1步 - 您需要使用“構造函數”,它將創建具有自己的名稱(和/或用戶ID)以及它們自己的屬性集的新用戶對象。
通過這種思路,您可以使構造函數包含諸如“用戶名”,“匹配點1”,“匹配點2”之類的屬性,然后是控制台記錄每個名稱的摘要及其匹配總點數的函數第1點和第2點。
第2步 - 您需要將構造函數放在一個循環中,該循環將通過數據庫查找您需要填充構造函數所需的屬性以吐出您正在尋找的信息所需的特定屬性。
所以...讓我們深呼吸,因為這是很多話......讓我們嘗試編碼。 我會以一種我認為可以讓您輕松插入自己的屬性/變量名稱的方式使用泛型屬性。
var user = function(name, match1, match2){
this.name = name;
this.match1 = match1;
this.match2 = match2;
this.pointTotal = function(match1, match2) {
console.log(match1 + match2);};
this.summary = function(){
console.log(name + " has a total of " + pointTotal + "
points.");};
}
代碼的“This”部分允許使用任何用戶名,而不僅僅是特定的用戶名。
好的,所以上面的代碼處理問題的構造函數部分。 現在,您需要使用唯一名稱創建多少用戶並不重要。
下一步是創建某種循環函數,該函數將遍歷數據庫並填寫創建每個用戶所需的屬性,以便您可以從每個用戶而不是一個用戶獲得總分。
再次,我將使用generic-ish屬性/變量名稱...
var key = childSnapshot.key;
while(i = 0; i < key.length + 1; i++) {
var user = function(name, match1, match2){
this.name = name;
this.match1 = match1;
this.match2 = match2;
this.pointTotal = function(match1, match2) {
console.log(match1 + match2);};
this.summary = function(){
console.log(name + " has a total of " + pointTotal + " points.");};
}
}
這是很多單詞,代碼是通用屬性名稱/變量和您使用的屬性名稱/變量的混合,但我確信我在正確的路徑上。
我非常有信心,如果您使用我提供的代碼和EXPLANATION,如果您插入自己的變量,您將獲得所需的解決方案。
最后,我只是想說,我真的希望有所幫助,如果不是,我想以這種或那種方式幫助解決問題,因為我需要這種做法。 我工作很奇怪,所以如果我不立即回答,我很可能在工作:(
祝你好運,我希望我幫助過!
看看這個實現。 不需要雲功能。
firebase().database().ref().on('value', function(snapshot) {
snapshot.forEach((user)=>{
user.forEach((matches)=> {
var total = 0;
matches.forEach((match)=> {
total += match.val().Points;
});
console.log(total);
});
});
})
如果密鑰是用戶的Id,為什么要添加另一個具有用戶名稱的嵌套對象? 您希望一個用戶擁有多個用戶名嗎? 這聽起來很奇怪,增加了復雜性,正如你可能已經注意到的那樣。 如果您需要在Firebase中的某個位置保留用戶名,建議您直接在用戶ID鍵下的某個位置專用用戶詳細信息部分。 以下是Firebase對象結構的JavaScript表示:
{
a1230scfkls1240: {
userinfo: {
username: 'Joe'
},
matches: {
asflk12405: {
points: 123
},
isdf534853: {
points: 345
}
}
}
}
現在,達到總積分似乎更直接,不是嗎? 😎
為了幫助您而不修改當前的數據庫結構,您只需循環遍歷數據庫中的所有userId +用戶名+匹配排列。 下面是一個實現這一目的的示例代碼,您不需要任何特殊的Firebase功能,只需要很好的舊JavaScript for循環:
const query = firebase.database().ref();
query.once('value')
.then(snapshot => {
const points = {}
const users = snapshot.val()
for (const userId of Object.keys(users)) {
const userprofile = users[userId]
for (const username of Object.keys(userprofile)) {
const user = userprofile[username]
for (const matchId of Object.keys(user)) {
const match = user[matchId]
// Store the points per user, per profile, or per both, depending on your needs
points[username] = points[username] === undefined
? points[username] = match.points
: points[username] += match.points
}
}
}
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.