[**@bondsports/general**](../README.md) • **Docs**

***

[@bondsports/general](../globals.md) / arrayToMapWithKey

# Function: arrayToMapWithKey()

> **arrayToMapWithKey**\<`T`, `K`\>(`array`, `keyFn`): `Map`\<`K`, `T`[]\>

Converts an array of objects into a Map using a specified key function to determine the keys.

## Type Parameters

• **T**

The type of the objects in the array.

• **K**

The type of the keys in the resulting Map.

## Parameters

• **array**: `T`[]

The array of objects to convert. Can be null or undefined, in which case an empty Map is returned.

• **keyFn**

A function that takes an object of type T and returns a key of type K.

## Returns

`Map`\<`K`, `T`[]\>

A Map where the keys are determined by the key function and the values are the objects themselves.

## Example

```ts
const input = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
const result = arrayToMapWithKey(input, item => item.name);
// result is a Map with:
//   'Alice' => { id: 1, name: 'Alice' }
//   'Bob' => { id: 2, name: 'Bob' }
```

## Defined in

[utils/array.util.ts:294](https://github.com/Bond-Sports/bondUtils/blob/545f16ceb055481acfd7641d7809c0f252d3a5e6/packages/general/src/utils/array.util.ts#L294)
