我有这样的数组:
如何从最大的id(不是status.id)从该数组中以角度返回状态标题?
我已经像这样更改了我的代码:
getData(id){
let url_detail = this.apiurl + `/${id}`;
this.http.get<any[]>(url_detail).pipe(
tap(data=>{
var status = data.reduce((item, curr) => {
return item.id < curr.id ? curr : item;
}).status.title;
return status;
}),
catchError(this.handleError)
);
}
当我这样调用该函数时:
console.log("status ", this.service.getData(1))
它只是返回未定义。请帮我。
@Alma,tap
不更改响应,请map
改用。此外,您需要订阅HttpClient.get返回的Observable才能检索结果。结合@dork的答案
getData(id){
let url_detail = this.apiurl + `/${id}`;
<!--see that you use return-->
return this.http.get<any[]>(url_detail).pipe(
<!--see that use map-->
map(data=>{
var status = data.reduce((item, curr) => {
return item.id < curr.id ? curr : item;
}).status.title;
return status;
}),
catchError(this.handleError)
);
}
//and
this.service.getData(1).susbcribe(res=>{
console.log("status ", res)
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句