@kingjs/linq.to-lookup

1.0.8 • Public • Published

@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

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

Analytics

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.8
    0
    • latest

Version History

Package Sidebar

Install

npm i @kingjs/linq.to-lookup

Weekly Downloads

0

Version

1.0.8

License

MIT

Unpacked Size

4.28 kB

Total Files

4

Last publish

Collaborators

  • kingces95