'Get array of ids from object array in firestore

in firestore I have a document with two strings and also an object array with [{email: ..., id: ..., nickname: ...} {...}] I use a subscription to get all users from that specific document. The next step is to extract all ids from that object array (called "users") in a new array.. But I have no idea how to do this. I try somethink like this:

this.group.forEach(element => console.log(element)

"this.group" is the subscription of that document. But this output display all content from this document and not the only array called (users)See attachement.

Hope anyone can help?



Solution 1:[1]

You can loop it and push it in a new array like this. See sample code below:

    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!");
    }

The same logic will be applied if you use it to the subscription of the document.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marc Anthony B