[英]Obtaining data from GET request and processing it (Angular 2)
我試圖從 Angular 2 中的GET
請求中獲取一系列信息。為此,我創建了一個服務,如下所示:
obtainInformation() {
// Obtain authorization token for the request
const authToken = localStorage.getItem('auth_token');
// Configure headers
const headers = new Headers();
headers.append('x-access-token', authToken);
// Execute request
return this.http.get(this.clubInfoUrl, { headers: headers })
.map(res => res.json())
.catch(this.handleError);
}
然后,我從info.component.ts
調用此服務,如下所示(為簡化info.component.ts
,我只添加了代碼的確切部分):
this.service.obtainInformation()
.subscribe(res=>{
console.log(res);
});
有了這個,我得到了一個包含兩個字符串和信息數組的對象(這是我要處理的數組),但我不確定如何選擇數組。
我試過JSON.stringify()
,但后來我不能做(例如):
res.club_info.id
請注意,club_info 是數組,而 id 是數組的元素之一。
有什么建議嗎?
先感謝您!
編輯: console.log(res)
輸出
確保在訪問時包含數組的索引。
你說:
請注意,club_info 是數組,而 id 是數組的元素之一。
club_info
是一個數組。 數組的第一個元素是一個 Javascript 對象,因此您需要提供數組索引來訪問該對象。 此外,屬性id
不存在,但也許您的意思是id_number
或_id
。
因此, res.club_info[0].id_number
顯然你得到了一個包含 1 個元素的數組:
你應該打電話給:
let first_element = res.club_info[0];
let first_element_id = first_element._id
祝你好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.