从响应中检查和分配对象的最佳方法是什么?

时间:2017-02-23 12:30:36

标签: javascript angular typescript

使用我的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
                    }

有时额外的东西没有价值。有时在附加内部的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);
    }

3 个答案:

答案 0 :(得分:0)

您可以使用以下功能来实现您的目标。

java.lang.AssertionError: 
Expecting:
 <"{"timestamp":1487852597527,"status":400,"error":"Bad Request","exception":"org.springframework.web.multipart.support.MissingServletRequestPartException","message":"Required request part 'file' is not present","path":"/api/upload"}">
to contain:
 <"success">
 (ignoring case)

然后像这样使用它

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

请参阅details

另一种方法是在实际创建对象之前执行seatingConcession: { parking: getSafe(() => data.concession.extras.parking), restrictedview: getSafe(() => data.concession.extras.restrictedview), wheelchair: getSafe(() => data.concession.extras.wheelchair), }

答案 1 :(得分:0)

你提到了,

&#34; 有时额外的东西没有价值。有时在附加内部的restrictedview没有价值&#34;

所以,这个条件会对你有帮助。

<强> data.concession.extras ? (data.concession.extras.restrictedview || data.concession.extras ) : null

以下是一个例子:

第一个示例有restrictedview,第二个示例没有。

&#13;
&#13;
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)
&#13;
&#13;
&#13;

请跑上面的SNIPPET

答案 2 :(得分:0)

Observables执行try...catch,因此对于数据结构,可以遵循以下模式:

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

但是对于嵌套结构,这将导致复杂的可观察层次结构,这很难理解。

因此,基本上可以使用此配方处理复杂的值路径:

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

这种情况可以通过Lodash/Underscore get或类似的辅助函数来方便地处理:

parking: _.get(data, 'concession.extras.parking', null)