Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJS: Removing fields from an object

Tags:

rxjs

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!

like image 217
Kemal Yalcinkaya Avatar asked Sep 10 '26 08:09

Kemal Yalcinkaya


1 Answers

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 }
like image 184
Balázs Édes Avatar answered Sep 13 '26 06:09

Balázs Édes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!