Function arrayToRecord

Converts an array of objects into a Record, using a specified key property or function, and an optional value property, array of properties, or value transformation function.

The type of objects in the array.

The type of the key property or key function.

The type of the value property, array of properties, or value function.

The array of objects to convert into a Record. If empty, an empty Record is returned.

The key property or function used to derive the keys for the Record. - If a string key, each object's property with this key is used as the record key. - If a function, it will be called with each item to compute the key. - The resulting keys will be converted to strings.

Optional. The value property, array of properties, or function used to derive the values for the Record. - If undefined, the entire object is used as the value. - If a single key, the corresponding property value is used. - If an array of keys, an object with only those properties is used. - If a function, it will be called with each item to compute the value.

A Record where: - The keys are derived from keyProp and converted to strings. - The values are derived from valueProps (or the entire object if valueProps is undefined).

If array is not an array.

If keyProp is a string and some objects in array are missing that property.

If keyProp function returns a value that cannot be converted to a string.

const data = [
{ id: 1, name: 'Alice', age: 25 },
{ id: 2, name: 'Bob', age: 30 }
];

// Case 1: No valueProps (entire object as value)
const recordById = arrayToRecord(data, 'id');
// Record<number, { id: number, name: string, age: number }>

// Case 2: Single valueProp
const recordWithAge = arrayToRecord(data, 'id', 'age');
// Record<number, number>

// Case 3: Array of valueProps
const recordWithNameAndAge = arrayToRecord(data, 'id', ['name', 'age']);
// Record<number, { name: string, age: number }>

// Case 4: Function as valueProps
const recordWithCustomValue = arrayToRecord(data, 'id', item => ({ fullName: item.name, isAdult: item.age >= 18 }));
// Record<number, { fullName: string, isAdult: boolean }>

// Case 5: Function as keyProp
const recordByCustomKey = arrayToRecord(data, item => item.name.toUpperCase(), 'age');
// Record<string, number>
  • Type Parameters

    • T
    • K extends string | number | symbol | (item: T) => PropertyKey

    Parameters

    • array: T[]
    • keyProp: K

    Returns Record<ArrayToRecordKey<K, T>, T>

  • Type Parameters

    • T
    • K extends string | number | symbol | (item: T) => PropertyKey
    • V extends string | number | symbol

    Parameters

    • array: T[]
    • keyProp: K
    • valueProps: V

    Returns Record<ArrayToRecordKey<K, T>, T[V]>

  • Type Parameters

    • T
    • K extends string | number | symbol | (item: T) => PropertyKey
    • V extends readonly (keyof T)[]

    Parameters

    • array: T[]
    • keyProp: K
    • valueProps: V

    Returns Record<ArrayToRecordKey<K, T>, Pick<T, V[number]>>

  • Type Parameters

    • T
    • K extends string | number | symbol | (item: T) => PropertyKey
    • V extends (item: T) => any

    Parameters

    • array: T[]
    • keyProp: K
    • valueProps: V

    Returns Record<ArrayToRecordKey<K, T>, ReturnType<V>>