Angular、转换 JSON、Rxjs Coverer、Typescript
Angular, Converting JSON , Rxjs Opertaor, Typescript
我从服务器
收到以下对象 (Survay)JSON 格式
/*****************************************/
{
"id": 870,
"title": "test survay ",
"questions": [
{
"id": 871,
"data": "question data 1"
},
{
"id": 874,
"data": "question data 2"
},
{
"id": 877,
"data": "question data 3"
}
],
"user": {
"id": 788,
"name": "mohamed",
"answres": [
{
"data":"answere question 1"
},
{
"data":"answere question 2"
},
{
"data":"answere question 3"
}
]
}
}
我使用了下面的代码
getSurvayByid(id: number) {
const headers = new HttpHeaders({
'Authorization': this.loadToken(),
'Content-Type': 'application/json'
});
return this.http.get<Survay>(this.host + "/survay/" + id, { headers: headers })
.pipe(map(resp => resp));
}
我想更改显示的格式:
我的 question:how 我可以使用 Rxjs 运算符获得这种格式吗?
(map, take, pipe...) 并更改方法 getSurvayByid(id: number)
{
"id": 870,
"title": "test survay ",
"questions": [
{
"id": 871,
"data": "question data 1",
"answer":"answere question 1"
},
{
"id": 874,
"data": "question data 2",
"answer":"answere question 2"
},
{
"id": 877,
"data": "question data 3",
"answer":"answere question 3"
}
],
"user": {
"id": 788,
"name": "mohamed"
}
}
假设答案顺序正确,您可以这样做:
return this.http.get<Survay>(this.host + "/survay/" + id, { headers: headers }).pipe(
map((resp: Survay): Survay => {
resp.questions.forEach((question, i) => question.answer = resp.answers.length == 0 ? '' : resp.answers[i].data);
return resp;
})
);
我假设 return 类型也是 Survay
。请替换为正确的 return 类型。
getSurvayByid(id).toPromise().then(s => {
s.questions.forEach((q,i)=>{this.answers=s.user.answres!
if(this.answers.length==0){
q.answer="";
}
else {
q.answer=this.answers[i].data;
}
})
}
我从服务器
收到以下对象 (Survay)JSON 格式/*****************************************/
{
"id": 870,
"title": "test survay ",
"questions": [
{
"id": 871,
"data": "question data 1"
},
{
"id": 874,
"data": "question data 2"
},
{
"id": 877,
"data": "question data 3"
}
],
"user": {
"id": 788,
"name": "mohamed",
"answres": [
{
"data":"answere question 1"
},
{
"data":"answere question 2"
},
{
"data":"answere question 3"
}
]
}
}
我使用了下面的代码
getSurvayByid(id: number) {
const headers = new HttpHeaders({
'Authorization': this.loadToken(),
'Content-Type': 'application/json'
});
return this.http.get<Survay>(this.host + "/survay/" + id, { headers: headers })
.pipe(map(resp => resp));
}
我想更改显示的格式:
我的 question:how 我可以使用 Rxjs 运算符获得这种格式吗? (map, take, pipe...) 并更改方法 getSurvayByid(id: number)
{
"id": 870,
"title": "test survay ",
"questions": [
{
"id": 871,
"data": "question data 1",
"answer":"answere question 1"
},
{
"id": 874,
"data": "question data 2",
"answer":"answere question 2"
},
{
"id": 877,
"data": "question data 3",
"answer":"answere question 3"
}
],
"user": {
"id": 788,
"name": "mohamed"
}
}
假设答案顺序正确,您可以这样做:
return this.http.get<Survay>(this.host + "/survay/" + id, { headers: headers }).pipe(
map((resp: Survay): Survay => {
resp.questions.forEach((question, i) => question.answer = resp.answers.length == 0 ? '' : resp.answers[i].data);
return resp;
})
);
我假设 return 类型也是 Survay
。请替换为正确的 return 类型。
getSurvayByid(id).toPromise().then(s => {
s.questions.forEach((q,i)=>{this.answers=s.user.answres!
if(this.answers.length==0){
q.answer="";
}
else {
q.answer=this.answers[i].data;
}
})
}