pluck-deep
Pluck values of a collection given a 'dot' separated string
module { ... }
Install
npm i -S pluck-deep
Usage
pluckDeep
accepts an array
or object
as an initial argument.
You can pick a value
or array of values
by passing in a selector
.
Selectors
For the purpose of pluckDeep
a selector
is a string
with object
value accessors separated with a .
. In other words, you can retrieve
a value just like you would on any object, except that these selectors
will automatically map
an array of values for you.
// Example selectors'planet.earth.size'; // returns a single value set a size'planet.saturn.moons.name'; // Will return an array of moon names
See below examples and tests for more selectors
Value plucking
var sel = 'mercury.venus';var o = mercury: venus: 'earth' ; var v = ;assert;
Array plucking
var sel = 'saturn.moons.name';var o = saturn: moons: name: 'titan' name: 'enceladus' name: 'rhea' ; var arr = ;var expect = osaturnmoons;assert;
Filterable
Add a filterable statment to filter out return values based on some comparable
var sel = 'system.planets.name[type=dwarf]';var o = system: planets: name: 'earth' type: 'ocean' name: 'pluto' type: 'dwarf' ; var v = ;;assert;assert; // With an objectvar sel = 'rings[type=gas]';var venus = type: 'gas' rings: false; var v = ;assert;