'Formatting objects in array from Firebase Database in React Native
I am testing pulling some data form Firebase Database, not sure if I am doing it correctly.
The data is stored like this in a Firebase Database db named "users"
[{
"2022-01-30": [
{
"name": "John",
"age": "22"
},
{
"name": "John",
"age": "21"
}
],
"2022-02-12": [
{
"name": "Greg",
"age": "23"
},
],
"2022-03-16": [
{
"name": "Anne",
"age": "24"
},
]
}]
Which I am using a simple react native app.
useEffect(() => {
loadItems()
}, []);
const loadItems = async () => {
await firebase.database().ref("users").get().then((snapshot) => {
snapshot.forEach((doc) => {
let val = doc.val();
val.forEach((element) => {
console.log(JSON.stringify(element));
});
})
})
}
which returns
{ name: John, age: 22 }
{ name: Mary, age: 21 }
{ name: Greg, age: 23 }
{ name: Anne, age: 24 }
You'll notice it's missing the commas separating the objects.
I would like to format it like this:
[{ name: John, age: 22 },
{ name: Mary, age: 21 },
{ name: Greg, age: 23 },
{ name: Anne, age: 24 }]
so I can display in a searchable flatlist and other places etc.
Any help appreciated 🙏
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
