@kingjs/linq.to-lookup
v1.0.8
Published
Creates a dictionary from a sequence where values are groups of elements keyed by a name common to all members of the group.
Downloads
35
Readme
@kingjs/linq.to-lookup
Creates a dictionary from a sequence where values are groups of elements keyed by a name common to all members of the group.
Usage
If the following people own the following pets
- Alice owns Fluffy
- Alice owns Spike
- Bob owns Tiger
then create a lookup from
- Alice to Fluffy and Spike
- Bob to Tiger
like this:
var toLookup = require('@kingjs/linq.to-lookup');
var sequence = require('@kingjs/enumerable.create');
var toArray = require('@kingjs/linq.to-array');
var lookup = toLookup.call(
sequence(
{ name: 'Alice', pet: 'Fluffy' },
{ name: 'Alice', pet: 'Spike' },
{ name: 'Bob', pet: 'Tiger' }
),
function(x) { return x.name; },
function(x) { return x.pet; }
)
for (var key in lookup)
lookup[key] = toArray.call(lookup[key]);
lookup;
result:
{
Alice: [ 'Fluffy', 'Spike' ],
Bob: [ 'Tiger' ]
}
API
declare function(
this: Enumerable,
keySelector: function(x): any,
valueSelector?: function(x): any,
): Dictionary
Interfaces
Enumerable
: See @kingjs/enumerable.define.Dictionary
: See @kingjs/dictionary.
Parameters
this
: Sequence to partition into named sequences.keySelector
: Selects a partition key from an element.valueSelector
: Transforms an element before inclusion in a partition. By default, selects the entire element.
Return Value
A partitioning of the sequence as a dictionary mapping partition names to partitions.
Install
With npm installed, run
$ npm install @kingjs/linq.to-lookup
Acknowledgments
Like Enumerable.ToLookup.
License
MIT