[英]Get array of ids from object array in firestore
在 firestore 中,我有一個包含兩個字符串的文檔以及一個帶有 [{email: ..., id: ..., nickname: ...} {...}] 的 object 數組我使用訂閱來獲取所有用戶該特定文件。 下一步是從新數組中的 object 數組(稱為“用戶”)中提取所有 ID。但我不知道該怎么做。 我嘗試這樣想:
this.group.forEach(元素 => console.log(元素)
“this.group”是該文檔的訂閱。 但是這個 output 顯示了這個文檔的所有內容,而不是唯一一個名為 (users) 的數組,見附件。
希望有人能幫忙嗎?
您可以像這樣循環它並將其推送到一個新數組中。 請參閱下面的示例代碼:
const docSnap = await getDoc(docRef);
const newArray = [];
if (docSnap.exists()) {
const users = docSnap.data().users
users.forEach(element => {
newArray.push(element.id);
})
// This will return an array of `id` from the `users` array.
console.log(newArray);
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
如果您將它用於文檔的訂閱,將應用相同的邏輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.