Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "utils/key-by/docs/types/index.d"

Index

Functions

Functions

Export assignment keyBy

  • keyBy(collection: Collection, fcn: Function, thisArg?: any): any
  • Converts a collection to an object whose keys are determined by a provided function and whose values are the collection values.

    Notes

    • When invoked, the input function is provided two arguments:

      • value: collection value
      • index: collection index
    • If more than one element in a collection resolves to the same key, the key value is the collection element which last resolved to the key.

    • Object values are shallow copies.

    Parameters

    • collection: Collection

      input collection

    • fcn: Function

      function to invoke

    • Optional thisArg: any

      execution context

    Returns any

    output object

    Example

    function toKey( value, index ) {
        console.log( '%d: %s', index, JSON.stringify( value ) );
        return value.name;
    }
    
    var collection = [
        { 'name': 'beep', 'a': 1 },
        { 'name': 'boop', 'b': 2 }
    ];
    
    var obj = keyBy( collection, toKey );
    // returns { 'beep': { 'name': 'beep', 'a': 1 }, 'boop': { 'name': 'boop', 'b': 2 } }