2017-02-23 71 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) 
相关问题