2

How to get only latest value from subscribe in angular 4?

this.AbcSrvice.value.subscribe(data => {console.log(data)})
danday74
  • 52,471
  • 49
  • 232
  • 283

1 Answers1

2

Use the last operator : https://www.learnrxjs.io/operators/filtering/last.html

import { last } 'rxjs/operators';


this.AbcSrvice.value
.pipe(last())
.subscribe(data => {console.log(data)});
David
  • 33,444
  • 11
  • 80
  • 118
  • its not useless, its impossible to answer the question without knowing more information – mast3rd3mon Sep 20 '18 at 14:23
  • why? he just wants the "latest value" emitted, who cares what the value is – danday74 Sep 20 '18 at 14:25
  • for all we know, the OP wants the latest entry in an array, or they could want the updated value, in both cases, you dont need the `.pipe()` – mast3rd3mon Sep 20 '18 at 14:28
  • @mast3rd3mon if it was the latest entry in an array, I'm hoping that the OP would not need post a question on SO... and this would not be related to `subscribe` either. However, I can understand that the question might seem unclear; and maybe I did not understand what OP meant either... just have to wait for them – David Sep 20 '18 at 14:49
  • Above code is not working for me – Shivaji Watekar Sep 20 '18 at 14:50
  • @ShivajiWatekar: `not working` meaning ...? – David Sep 20 '18 at 14:51
  • @David I am subscriblng a boolean BehaviorSubject, so i want only latest value or last value from observable – Shivaji Watekar Sep 20 '18 at 14:52
  • @ShivajiWatekar And what do you get using `last`? – David Sep 20 '18 at 15:10