kingjs/descriptor.nested.reduce
@Accumulates the values of leafs at specific paths using a callback.
Usage
Return an array of pet names like this:
var reduce = require('@kingjs/descriptor.nested.reduce');
var people = {
alice: {
pet: 'tiger'
},
bob: {
pet: 'snuggles'
},
chris: {
pet: 'spike'
}
}
var tree = {
'*': { pet: null }
}
reduce(people, tree, (a, o) => {
a.push(o);
return a;
}, []);
result:
[ 'tiger', 'snuggles', 'spike' ]
API
declare function reduce(
tree: NestedDescriptor,
paths: NestedDescriptor,
callback: (accumulator, value) => any,
initialValue?,
thisArg?
): any
Interfaces
-
NestedDescriptor
: see @kingjs/descriptor/nested
Parameters
-
tree
: A tree whose leaf values that correspond to a path inpaths
are accumulated. -
paths
: Paths to search intree
. If the path is found, then it's value is accumulated. Any property named'*'
found inpaths
is replicated with properties whose names correspond to properties oftree
not already present as siblings of'*'
. -
callback
: A callback invoked on each value accumulated. Return the newly accumulated value.-
accumulator
: The accumulated value so far. Will benull
by default. -
value
: The leaf being currently accumulated.
-
-
initialValue
: Value to use as the first argument of the callback. If no initial value is supplied, the first value visited will be used. -
thisArg
: Thethis
argument to pass tocallback
.
Returns
Returns the accumulated value or null
if no values were accumulated..
Install
With npm installed, run
$ npm install @kingjs/descriptor.nested.reduce
License
MIT