Example Filter,sort,map Observable stream
Example 1
sort array of object alphapitaclly . Array of countries
findAllCountry(token: string): Observable<ICountry[]> {
const headerInfo = new HttpHeaders({
Authorization: token,
});
return this.http
.get<ICountry[]>(
`${environment.backendUrl}/crm-operations/lookup/country`,
{ headers: headerInfo }
)
.pipe(
tap((data) =>
data.sort((a, b) => {
if (a.couShortNameE < b.couShortNameE) {
return -1;
}
if (a.couShortNameE > b.couShortNameE) {
return 1;
}
})
)
);
}
Example 2
async findAll(token: string): Promise<Observable<ComprehensiveLimit[]>> {
let customer = await getSessionInfo("customer");
const headerInfo = new HttpHeaders({
Authorization: token,
});
let compRef = customer.compRef;
return this.http
.get<ComprehensiveLimit[]>(
`${this.url}/crm-operations/application/compRef/${compRef}`,
{ headers: headerInfo }
)
.pipe(
map(items => items.filter(item => item.status === 'SAV'))
, map((responseArray) => {
return responseArray.map((record) => {
return new ComprehensiveLimit(
record.clRef,
record.transType,
record.riskRef,
record.hsCode,
record.status,
record.customer,
record.currency,
record.whoColumns,
record.comprehensiveLimitsDetailsEntity,
record.comprehensiveLimitsDetailsEntity.length
);
});
})
,tap(result => result.sort( (a,b)=> b.clRef -a.clRef
)
)
);
}
Comments