Function arrayToMap

Converts an array of objects into a Map, 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 Map. If empty, an empty Map is returned.

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

Optional. The value property, array of properties, or function used to derive the values for the Map. - 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 Map where: - The keys are derived from keyProp. - 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.

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

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

// Case 2: Single valueProp
const mapWithAge = arrayToMap(data, 'id', 'age');
// Map<number, number>

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

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

// Case 5: Function as keyProp
const mapByCustomKey = arrayToMap(data, item => `${item.name}-${item.age}`);
// Map<string, { id: number, name: string, age: number }>
  • Type Parameters

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

    Parameters

    • array: T[]
    • keyProp: K

    Returns Map<ArrayToMapKey<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 Map<ArrayToMapKey<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 Map<ArrayToMapKey<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 Map<ArrayToMapKey<K, T>, ReturnType<V>>