2017-02-23 74 views
0

用我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); 
    } 
+0

如果data.concession。 extras.parking爲null,不需要那樣做,是嗎? – paqash

+0

試試這個,它會同時滿足'data.concession.extras? (data.concession.extras。restrictedview || null):null' – Sravan

+0

@Satpal同一問題 – user2280016

回答

0

您可以使用下面的函數來實現你想要的。

function getSafe(fn) { 
    try { 
     return fn(); 
    } catch (e) { 
     return null; 
    } 
} 

然後使用它像這樣

seatingConcession: { 
    parking: getSafe(() => data.concession.extras.parking), 
    restrictedview: getSafe(() => data.concession.extras.restrictedview), 
    wheelchair: getSafe(() => data.concession.extras.wheelchair), 
} 

details

另一種方法是在實際創建對象之前執行data.concession.extras = data.concession.extras || {}

+0

是否適用於打字稿? – user2280016

+0

@ user2280016是的。 – str

+0

找不到名字getSafe – user2280016

0

你提到,

有時演員沒有價值。有時restrictedview演員裏面沒有價值

所以,這種情況下會幫助你。

data.concession.extras ? (data.concession.extras.restrictedview || data.concession.extras) : null

下面是一個例子:

第一個例子具有restrictedview和第二個例子卻沒有。

data = {} 
 
data.concession = { 'extras' : {} } 
 
data.concession.extras = { 'restrictedview' : 'restrictedview value'} 
 

 
data2 = {} 
 
data2.concession = { 'extras' : 'extras value' } 
 

 

 
var output = data.concession.extras ? (data.concession.extras.restrictedview || data.concession.extras) : null 
 

 
var output2 = data2.concession.extras ? (data2.concession.extras.restrictedview || data2.concession.extras) : null 
 

 

 
console.log(output) 
 
console.log(output2)

請執行上面的代碼中

0

觀測量做try...catch,因此對於數據結構可以遵循的模式:

data$ 
.map(data => data.complex.path || null) 
.catch(() => Observable.of(null)) 

但對於嵌套結構這將導致我n難以理解的複雜的可觀察等級。

所以基本上可以複雜路徑請客值隨這個食譜:

parking: ((data.concession || {}).extras || {}).parking || null 

這種情況可方便地通過Lodash/Underscore get或類似的輔助函數處理:

parking: _.get(data, 'concession.extras.parking', null) 
相關問題