1

I have a function in my angular component that receives an observable. I need to modify the object inside the observable and then 'put it back inside' the observable again and return it. This is my code so far:

myImmobili =  Observable<Valutazione[]>;
newImmobile(immobili: Observable<Valutazione[]>) {

    immobili.subscribe(
      imm => { 
        console.log(imm);
        imm.push(new Valutazione());
        console.log(imm);
        this.myImmobili = //some code here
      }
    );
  }

Can anyone give me a hint? Maybe there's even a better way to modify my array without subscribing to it, but I couldn't find it. Thanks.

EDIT

newImmobile() {

    this.immobili.subscribe(i => console.log('before', i));
    this.immobili.map(imm => {
      imm.push(new Valutazione());
      console.log('inside', imm);
    }
    );
    this.immobili.subscribe(i => console.log('after', i));
  }

Now it skips completely the map function. The logs 'before' and 'after' shows the same array, and the 'inside log' doesn't show.

3
  • Do you want to change whole object or just modify the values of array based on some condition? Commented Sep 4, 2017 at 12:51
  • Only the values of the array, as I'm doing inside the subscribe() call. I need to return an Observable that contains the modified array. Commented Sep 4, 2017 at 12:54
  • 2
    Use 'map' operator to change the values. Commented Sep 4, 2017 at 12:56

1 Answer 1

1

Instead of subscribing you can use map:

immobili.map(
  imm => { 
    console.log(imm);
    imm.push(new Valutazione());
    console.log(imm);
    this.myImmobili = //some code here
  }
);
Sign up to request clarification or add additional context in comments.

3 Comments

I changed my code as you and @Basavaraj Bhusani suggest but it skips completely the map operator execution.
you need to subscribe atleast once at the end point. You can't use only map. it has to be coupled with subscribe.
That was it. Thanks.

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.