0

I received the following object (Survay) JSON format from server

/**************************************/

    {
        "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"
              
              }
              
              ]
           
    
        }
 
   }

I used the following code

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

I would like to change this format as it’s displayed:

My question:how I can get this format using the Rxjs operators ? (map, take, pipe...) and change the method 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"
        
        }
    }

2 Answers 2

1

Assuming the answers are in correct order, you can do it like this:

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

I'm assuming the return type is also Survay. Please replace with correct return type.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for that answer but the type of return getSurvayByid(id: number ) it change from Observable<survay> a Observable<void> so to correct this trouble apply the forEach inside toPromise().then or subscribe() see the answer this below
You should fix such type issues by properly using typescript rather than converting to Promises. RxJS is very powerful and using toPromise cripples it's potential. I'll update my answer with proper types.
0
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;
            }
           
            
            })
          
           
        }

1 Comment

While this solution works, I'll suggest to fix type errors by properly using typescript rather than using toPromise. With this solution you cannot use RxJS operators.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.