0

There is small correction. What we are returning is Object[] and not Object. The method signature is also changed as below - I have a method which returns Observable as below-

 getData(url: string): Observable<Object[]> {

    return this.http.get<Object[]>(url);
  }

Where the Object is like below :

{

  property1 : value1 
  property2 : value2 
  property3 : "" // no value right now 
}

What I want to do is :

return Observable<Object[]> (Please note same object)

Where the Object is like below :

{

  property1 : value1 
  property2 : value2 
  property3 : value1 + value2; //combine property1 and property2 values 
} 

How can we achieve this ?

I know we have to use rxjs map operator but how do we use it ?

The url Transforming Observable with .map talks about transforming Array but not Map

How can we achieve this using Map operator on Observable ?

4
  • Do you mean like this? Commented Nov 20, 2019 at 5:56
  • .map(obj => ({...obj, property3: obj[property1] + obj[property2]}) Commented Nov 20, 2019 at 5:56
  • 1
    Your method signature is not TypeScript, looks more like C# or Java, so this can't be valid Angular code. What problem do you have with returning the object as you described? Commented Nov 20, 2019 at 5:57
  • The question is edited . Can you take a look please ? Commented Nov 20, 2019 at 6:23

1 Answer 1

1

Okay, you need to map data of Observable before it's been subscribed.

There's a function called pipe that used to modify data of Observable and returns an Observable.

For your case:

Observable<Object> getData(){
    // let assume your observable is called getObs
    return getObs.pipe(
        map(objs => objs.map(obj => {
            obj[property3] = obj[property1] + obj[property2]);
            return objs;
        })
        )
    )
}

Btw, all rxjs/operators must be called inside pipe function, not just map.

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

1 Comment

Okay, check it now

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.