pols-utils - v5.3.11
    Preparing search index...

    Function indexBy

    • Indexes an array.

      Type Parameters

      • T

        The type of elements in the array.

      • K = never

      Parameters

      • array: T[]

        Target array.

      • setterPropertyName: (element: T, index?: number) => string

        Function that returns the key of the index.

      • OptionalformatElement: (element: T, index?: number) => K

        Transformation elements.

      Returns Record<string, [K] extends [never] ? T : K>

      An object with the indexed elements of the original array.

      const pets = [
      { type: 'dog', name: 'Fido' },
      { type: 'dog', name: 'Chacalin' },
      { type: 'cat', name: 'Mr. Brown' },
      { type: 'cat', name: 'Night' },
      ]
      console.log(PUtilsArray.index(pets, pet => pet.type))
      // {
      // dog: { type: 'dog', name: 'Fido' },
      // cat: { type: 'cat', name: 'Mr. Brown' },
      // }