esseara
esseara

Reputation: 880

Angular - modify observable value

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.

Upvotes: 1

Views: 3513

Answers (1)

Sumit Agarwal
Sumit Agarwal

Reputation: 4306

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

Upvotes: 1

Related Questions