AngularFire Firebase 从 observable 转换数组
AngularFire Firebase transform array from observable
我使用 angular CLI,angularFire 和 Firebase。
我在真实数据库中有此数据:
我使用 observale 从 firebase 获取数据:
//Service.ts
import { Injectable } from '@angular/core';
import { Patient } from '../models/patient.model';
import {PPS } from '../models/pps.model';
import { AngularFireDatabase, AngularFireList, AngularFireObject} from 'angularfire2/database';
@Injectable({
providedIn: 'root'
})
export class PPSsService {
ppss: AngularFireList<any>;
constructor(private database: AngularFireDatabase) {this.ppss = database.list('ppss');}
getPPSByPatientid(Patientid: string){
return this.database.list('/ppss', ref => ref.orderByChild("Patientid").equalTo(Patientid)).valueChanges();
}
}
// component.ts
export class PpsComponent implements OnInit {
patientid: string;
patientToDisplay;
diagnosticsToDisplay;
ppssToDisplay;
traitement: string;
data1: any[];
config1: GanttChartConfig;
elementId1: string;
constructor(
private route: ActivatedRoute,
private location: Location,
private patientsService: PatientsService,
private diagnosticsService: DiagnosticsService,
private ppssService: PPSsService,
private router: Router,
public dialog: MatDialog,
){ }
ngOnInit() {
this.route.params.forEach((urlParameters) => {
this.patientid = urlParameters['id']; });
this.patientToDisplay = this.patientsService.getSinglePatient(this.patientid);
this.diagnosticsToDisplay = this.diagnosticsService.getDiagnosticByPatientid(this.patientid);
this.ppssToDisplay = this.ppssService.getPPSByPatientid(this.patientid);
console.log(this.ppssToDisplay);
this.data1 = [[ 'traitement','start', 'end'],
[ 'Chirurgie', new Date(2017, 3, 29), new Date(2017, 3, 30)],
[ 'Chimiothérapie', new Date(2017, 2, 4), new Date(2018, 2, 4)],
[ 'Radiothérapie', new Date(2017, 2, 4), new Date(2018, 2, 4)]];
this.config1 = new GanttChartConfig('', '', '',new Date (),new Date ());
this.elementId1 = 'myGanttChart';
}
而且我可以在模板中显示数据...
但现在我 component.ts 需要一个这样的数组:
this.ppssToDisplay = [['typetraitement', 'traitement','datedebut', 'datefin']]
我尝试用您的代码替换 data1,但出现错误 ppssToDisplay.map is not a fonction...
您可以使用 Object.values()
or Object.keys()
。
this.ppssToDisplay.map(obj => Object.keys(obj))
// [['typetraitement', 'traitement','datedebut', 'datefin'], ...]
this.ppssToDisplay.map(obj => Object.values(obj))
// [["-LFw...", "2018-06-...", "2018-06...", ....],...]
您可能也有兴趣查看这个先前回答的问题 about .map()
for Objects。
编辑
针对您的评论,您也可以这样做:
let interestingFields = ['typetraitement', 'traitement','datedebut', 'datefin'];
this.ppssToDisplay.map(obj => interestingFields.map(field => obj[field]))
// only the values of your defined fields
我使用 angular CLI,angularFire 和 Firebase。
我在真实数据库中有此数据:
//Service.ts
import { Injectable } from '@angular/core';
import { Patient } from '../models/patient.model';
import {PPS } from '../models/pps.model';
import { AngularFireDatabase, AngularFireList, AngularFireObject} from 'angularfire2/database';
@Injectable({
providedIn: 'root'
})
export class PPSsService {
ppss: AngularFireList<any>;
constructor(private database: AngularFireDatabase) {this.ppss = database.list('ppss');}
getPPSByPatientid(Patientid: string){
return this.database.list('/ppss', ref => ref.orderByChild("Patientid").equalTo(Patientid)).valueChanges();
}
}
// component.ts
export class PpsComponent implements OnInit {
patientid: string;
patientToDisplay;
diagnosticsToDisplay;
ppssToDisplay;
traitement: string;
data1: any[];
config1: GanttChartConfig;
elementId1: string;
constructor(
private route: ActivatedRoute,
private location: Location,
private patientsService: PatientsService,
private diagnosticsService: DiagnosticsService,
private ppssService: PPSsService,
private router: Router,
public dialog: MatDialog,
){ }
ngOnInit() {
this.route.params.forEach((urlParameters) => {
this.patientid = urlParameters['id']; });
this.patientToDisplay = this.patientsService.getSinglePatient(this.patientid);
this.diagnosticsToDisplay = this.diagnosticsService.getDiagnosticByPatientid(this.patientid);
this.ppssToDisplay = this.ppssService.getPPSByPatientid(this.patientid);
console.log(this.ppssToDisplay);
this.data1 = [[ 'traitement','start', 'end'],
[ 'Chirurgie', new Date(2017, 3, 29), new Date(2017, 3, 30)],
[ 'Chimiothérapie', new Date(2017, 2, 4), new Date(2018, 2, 4)],
[ 'Radiothérapie', new Date(2017, 2, 4), new Date(2018, 2, 4)]];
this.config1 = new GanttChartConfig('', '', '',new Date (),new Date ());
this.elementId1 = 'myGanttChart';
}
而且我可以在模板中显示数据...
但现在我 component.ts 需要一个这样的数组:
this.ppssToDisplay = [['typetraitement', 'traitement','datedebut', 'datefin']]
我尝试用您的代码替换 data1,但出现错误 ppssToDisplay.map is not a fonction...
您可以使用 Object.values()
or Object.keys()
。
this.ppssToDisplay.map(obj => Object.keys(obj))
// [['typetraitement', 'traitement','datedebut', 'datefin'], ...]
this.ppssToDisplay.map(obj => Object.values(obj))
// [["-LFw...", "2018-06-...", "2018-06...", ....],...]
您可能也有兴趣查看这个先前回答的问题 about .map()
for Objects。
编辑
针对您的评论,您也可以这样做:
let interestingFields = ['typetraitement', 'traitement','datedebut', 'datefin'];
this.ppssToDisplay.map(obj => interestingFields.map(field => obj[field]))
// only the values of your defined fields