用我angular2的应用程序,我得到了响应,并分配给对象,如下所示,从响应中检查并分配给对象的最佳方法是什么?
seatingConcession: {
parking: data.concession.extras.parking ? data.concession.extras.parking : null,
restrictedview: data.concession.extras.restrictedview ? data.concession.extras.restrictedview : null,
wheelchair: data.concession.extras.wheelchair ? data.concession.extras.wheelchair : null
}
有时演员不具备的价值。有时在extras内的restrictedview没有价值。什么是检查和分配默认值的最佳方法。 整个代码:
this.eventService.getListingsByEventId(this.eventId).subscribe(listresults => {
this.bindListing(listresults);
}, error => this.errorMessage = error);
}
bindListing(listres: any[]) {
let price_table = {};
let section_table = {};
listres.forEach((data) => {
data.ticket.seating.forEach((seat: any) => {
// tslint:disable-next-line:max-line-length
this.listings.push({
section: seat.section, selling: data.price.selling, amount: data.ticket.amount, type: data.ticket.type, row: seat.row, category: seat.category,
seatingConcession: {
parking: data.concession.extras ? (data.concession.extras.restrictedview || null) : null,
restrictedview: data.concession.extras.restrictedview || null,
wheelchair: data.concession.extras.wheelchair || null
},
deliveryconcession: {
instantdownload: data.delivery.instantdownload || null,
readytoship: data.delivery.readytoship || null,
unespecifiedshipment: data.delivery.unspecifiedshipment || null
}
});
// this.listings.push({ section: seat.section, selling: data.price.selling, amount: data.ticket.amount, type: data.ticket.type, row: seat.row, category: seat.category});
// tslint:disable-next-line:curly
if (!price_table.hasOwnProperty(data.price.selling))
price_table[data.price.selling] = [];
price_table[data.price.selling].push(data);
// tslint:disable-next-line:curly
if (!section_table.hasOwnProperty(seat.section))
section_table[seat.section] = [];
section_table[seat.section].push(data);
});
});
服务JS:
getListingsByEventId(EventID: string): Observable<ListingSeller[]> {
let apiurl = this.appConfig.getAPIUrl() + '/getListingsByEventId';
return this.http
.get(apiurl + queryString)
.map(this.extractData)
.catch(this.handleErrors);
}
如果data.concession。 extras.parking为null,不需要那样做,是吗? – paqash
试试这个,它会同时满足'data.concession.extras? (data.concession.extras。restrictedview || null):null' – Sravan
@Satpal同一问题 – user2280016