Lets say I have an object with 4 fields like this:
obj = {a:"3", b:"7", c:"10", d:"123"}
and I need to 'narrow' it to an object with fewer fields, like that:
newObj = {a:"3", c:"10"}
I know this can be done by deleting the fields (ie. delete obj.b )
My question is, can this be done with RxJS? And if yes, how???
Thanks a lot!
You if you have an Observable, that emits the shape above, you can do something like this:
Rx.Observable.of({ a:"3", b:"7", c:"10", d:"123" })
.map(({ a, c }) => ({ a, c }))
In the mapping function I destructure the object, then create an object literal with the shorthand syntax. Mind that these are es6/7 features.
But you really don't need rxjs or Observables to do this:
const original = { a:"3", b:"7", c:"10", d:"123" }
const changed = { a: original.a, c: original.c }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With