Hey everyone, I'm currently facing an issue with formatting my Firebase data into an array.
Below is the service where I am retrieving data from Firebase:
File name: subcategory.service.ts
export class SubcategoryService {
subcategoryRef: AngularFireList<any> = null;
constructor(private db: AngularFireDatabase) {
this.subcategoryRef = db.list("/subCategory");
}
getSubCategoriesById(inp_subCatid: string): Observable<any[]> {
return this.subcategoryRef.snapshotChanges().pipe(
take(1),
map(changes => changes.map(c =>
({ key: c.payload.key, ...c.payload.val() })
).filter((subCat) =>
subCat.catid === inp_subCatid
)
));
}
}
I am calling the function getSubCategoriesById() in the following file:
File name: subcategory.page.ts
this.SubcategoryService.getSubCategoriesById("cat01")
.subscribe(subCategories =>
this.subCat = Object.values(subCategories)
)
The structure of the object that I am retrieving appears as shown in https://i.sstatic.net/GYGA9.png. There is an array containing one object which holds my desired objects.
However, my goal is to format the data into the following structure:
[
alcoholic:{
active:true,
ageRating: true,
category: "cat01"
...
},
warmdrinks:{
active:true,
ageRating: false,
category: "cat01"
...
},
softdrinks:{
active:true,
ageRating: false,
category: "cat01"
...
},
]
This way, I will have an array with three objects inside it.
My Firebase database for this case is structured as shown in https://i.sstatic.net/HdCGu.png.
If anyone can provide assistance or requires more information, please let me know. Thank you!