indexBy
ts
function Array.indexBy<T extends object, U extends PropertyKey>(
target: readonly T[],
by: (
value: NoInfer<T>,
idx: number,
target: readonly NoInfer<T>[],
) => U,
): Record<U, T>Creates a record by indexing the target array using the by function to generate keys. Optionally transforms values using the transform function.
Example
ts
import { Array } from "@monstermann/array";
const users = [
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" },
];
Array.indexBy(users, (user) => user.id);
// { 1: { id: 1, name: 'Alice' }, 2: { id: 2, name: 'Bob' } }
Array.indexBy(
users,
(user) => user.id,
(user) => user.name,
); // { 1: "Alice", 2: "Bob" }ts
import { Array } from "@monstermann/array";
pipe(
users,
Array.indexBy((user) => user.id),
); // { 1: { id: 1, name: 'Alice' }, 2: { id: 2, name: 'Bob' } }
pipe(
users,
Array.indexBy(
(user) => user.id,
(user) => user.name,
),
); // { 1: "Alice", 2: "Bob" }