I am transforming the data received from my firebase database into a format appropiate to use on a SectionList.
I have been able to create some part of the data structure but have not been able to associate the data.
The format of the SectionList is the following:
[
{
index: 0,
title: "Pan simple",
data: [
{
id: 0,
},
{
id: 1,
}
]
},
{
index: 1,
title: "Pan dulce",
data: [
{
id: 0,
}
]
}
]
And the format I receive from my database is the following:
"storeName": {
products: {
'-Lo7D2Y8ZVEXnJb5LMvd': {
name: 'Pan de molde',
category: 'Molde'
},
'-Lo7C4HAkeE0cssxg9E-': {
name: 'Bollo de Hamburguresa',
category: 'Hamburguesa'
}
},
category: {
Molde: {
'-Lo7D2Y8ZVEXnJb5LMvd': true
},
Hamburguesa: {
'-Lo7C4HAkeE0cssxg9E-': true
}
}
}
Where the "title" are the categories keys and the "data" are the objects on the products branch. I have this function to receive the data and transform it:
export default (props, storeName) => {
firebase
.database()
.ref(`Store/${storeName}`)
.on("value", function(snapshot) {
if (snapshot.val() != null) {
var values = snapshot.val() || {};
var dataFormat = [];
var x = 0;
for (var i in values.category) {
var data = Object.assign({
data: [],
index: x,
category: i
});
dataFormat.push(data);
x++;
}
console.log("Data", dataFormat);
}
});
};
Currently the output is
[
{ data: [], index: 0, category: 'Molde' },
{ data: [], index: 1, category: 'Hamburguesa' }
]
And the expected output will be:
[
{
data: [
{
id: 'Lo7D2Y8ZVEXnJb5LMvd',
name: 'Pan de molde',
category: 'Molde'
}
],
index: 0,
category: 'Molde'
},
{
data: [
{
id: '-Lo7C4HAkeE0cssxg9E-',
name: 'Bollo de Hamburguresa',
category: 'Hamburguesa'
}
],
index: 1,
category: 'Hamburguesa'
}
]
How can I put the appropiate data in each section?
Pan de moldehas an ID of 0 in your expected output).