角度/火力地基格式从火力地基检索到数组的数据 [英] angular/firebase format retrieved data from firebase into array

查看:209
本文介绍了角度/火力地基格式从火力地基检索到数组的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我无法将Firebase数据格式化为数组

hello guys i have a problem to format my firebase-data into array

这是我从Firebase检索数据的服务

this is my service where im retrieving data from firebase

文件名:subcategory.service.ts

filename: 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
      )
    ));
  }
}

在以下文件中调用函数getSubCategoriesById()

im calling the function getSubCategoriesById() in the following file

文件名:subcategory.page.ts

filename: subcategory.page.ts

    this.SubcategoryService.getSubCategoriesById("cat01")
      .subscribe(subCategories =>
        this.subCat = Object.values(subCategories)
    )

正在检索的对象的结构如下

the structure of the object what im retrieving looks like this

有一个数组,其中有一个对象,其中我的目标对象是

there is an array in which there is one object in which my destination objects are

但是我想将数据格式化为以下结构

but i would like to format the data as the following structure

[
 alcoholic:{
             active:true,
             ageRating: true,
             catgeory: "cat01"
             ...
            },
 warmdrinks:{
             active:true,
             ageRating: false,
             catgeory: "cat01"
             ...
            },
 softdrinks:{
             active:true,
             ageRating: false,
             catgeory: "cat01"
             ...
            },
]

所以我有一个内部有3个对象的数组.

so that i have an array with 3 objects inside.

在这种情况下,我的Firebase数据库如下所示

my firebase database for this case looks like this

希望有人可以帮助我,如果有人需要更多信息,请告诉我

hope someone can help me, if someone needs more information, please let me know

推荐答案

您可以将映射添加到最后一个数组映射上的链中,以拾取所需的属性:

You can add a map to the chain on the last array map to pickup the attributes you want:

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
      ).map(({alcoholic,softdrinks,warmdrinks,...rest}) => ({
        alcoholic,softdrinks,warmdrinks
      })
    ));
  }

这篇关于角度/火力地基格式从火力地基检索到数组的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆