Quantcast
Channel: User Simon_Weaver - Stack Overflow
Viewing all articles
Browse latest Browse all 116

Answer by Simon_Weaver for What's the difference between map and pluck in RxJS?

$
0
0

The docs say

Pluck : Like map, but meant only for picking one of the nestedproperties of every emitted object.

Therefore, let's say you have

[  { name: 'Joe', age: 30, job: { title: 'Developer', language: 'JavaScript' } },  { name: 'Sarah', age: 35 }]

and you want a list of all job titles. pluck returns

"Developer" , undefined

Using map would be kind of a pain (because of the nullability of job), but with 'pluck' you can write pluck('job', 'title') and it will traverse the tree looking for job.title - and won't fail if job is null.

Example taken from : https://www.learnrxjs.io/learn-rxjs/operators/transformation/pluck

https://jsfiddle.net/btroncone/n592m597/

P.S: Please note that pluck has been deprecated

Deprecation Notes

Use map and optional chaining: pluck('foo', 'bar') is map(x => x?.foo?.bar). Will be removed in v8.


Viewing all articles
Browse latest Browse all 116

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>