簡體   English   中英

Angular,轉換 JSON,Rxjs 操作員,Z9327DAFFB330417F4027934CC9Z5F

[英]Angular, Converting JSON , Rxjs Opertaor, Typescript

我從服務器收到了以下 object ( 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));
    
        }

我想在顯示時更改此格式:

我的問題:如何使用 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;
       })
    );

我假設返回類型也是Survay 請用正確的返回類型替換。

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;
            }
           
            
            })
          
           
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM