2021年3月28日星期日

How to merge query-results/objects by id in an array in a React app using Firebase Firestore

I have the following query result from a Firestore snapshot which is basically chat messages from 3 people:

{timestamp: "October 25th 2020, 11:13:59 am", name: "John Doe", email: "john@example.com", uid: "pUNIrnZaMWXC7Dai5aHapZFeght9", content: "Hello"}    {timestamp: "October 25th 2020, 11:14:01 am", name: "John Doe", email: "john@example.com", uid: "pUNIrnZaMWXC7Dai5aHapZFeght9", content: "World"}    {timestamp: "October 25th 2020, 11:14:03 am", name: "John Doe", email: "john@example.com", uid: "pUNIrnZaMWXC7Dai5aHapZFeght9", content: "How are you?"}      {timestamp: "October 25th 2020, 11:15:10 am", name: "Jane Jones", email: "jane@example.com", uid: "cARRYrnZaMWXC9Dai5aHapZFeght9", content: "I am good, you?"}    {timestamp: "October 25th 2020, 11:15:14 am", name: "Jane Jones", email: "jane@example.com", uid: "cARRYrnZaMWXC9Dai5aHapZFeght9", content: "Nice weather"}    {timestamp: "October 25th 2020, 11:15:17 am", name: "Jane Jones", email: "jane@example.com", uid: "cARRYrnZaMWXC9Dai5aHapZFeght9", content: "See you!"}      {timestamp: "October 25th 2020, 11:15:20 am", name: "Tom Adams", email: "tom@example.com", uid: "sVOUIrnZaMWXC9Dai5aHapZFejtr9", content: "Finally, spring time!"}    {timestamp: "October 25th 2020, 11:15:24 am", name: "Tom Adams", email: "tom@example.com", uid: "sVOUIrnZaMWXC9Dai5aHapZFejtr9", content: "I agree!"}    {timestamp: "October 25th 2020, 11:15:31 am", name: "Tom Adams", email: "tom@example.com", uid: "sVOUIrnZaMWXC9Dai5aHapZFejtr9", content: "Let's do it!"}  

What I want to achieve is placing the results in an key/value object like the following:

    {            uid: pUNIrnZaMWXC7Dai5aHapZFeght9            data: [        {timestamp: "October 25th 2020, 11:13:59 am", name: "John Doe", email: "john@example.com", uid: "pUNIrnZaMWXC7Dai5aHapZFeght9", content: "Hello"}            {timestamp: "October 25th 2020, 11:14:01 am", name: "John Doe", email: "john@example.com", uid: "pUNIrnZaMWXC7Dai5aHapZFeght9", content: "World"}            {timestamp: "October 25th 2020, 11:14:03 am", name: "John Doe", email: "john@example.com", uid: "pUNIrnZaMWXC7Dai5aHapZFeght9", content: "How are you?"}            ]}            or             {            uid: cARRYrnZaMWXC9Dai5aHapZFeght9           data: [      {timestamp: "October 25th 2020, 11:15:10 am", name: "Jane Jones", email: "jane@example.com", uid: "cARRYrnZaMWXC9Dai5aHapZFeght9", content: "I am good, you?"}            {timestamp: "October 25th 2020, 11:15:14 am", name: "Jane Jones", email: "jane@example.com", uid: "cARRYrnZaMWXC9Dai5aHapZFeght9", content: "Nice weather"}            {timestamp: "October 25th 2020, 11:15:17 am", name: "Jane Jones", email: "jane@example.com", uid: "cARRYrnZaMWXC9Dai5aHapZFeght9", content: "See you!"}      ]            }  
https://stackoverflow.com/questions/66846661/how-to-merge-query-results-objects-by-id-in-an-array-in-a-react-app-using-fireba March 29, 2021 at 06:06AM

没有评论:

发表评论