toRecord
Converts an array of objects into a record (object) where each key is determined by the specified selector and the value is determined by the provided function.
Import
import { toRecord } from "@aa-sdk/core";
Usage
import { toRecord } from "@aa-sdk/core";
import { sepolia, mainnet } from "viem/chains";
const addressesByChain = toRecord([sepolia, mainnet], "id", () => "0x..."); // { [sepolia.id]: "0x...", [mainnet.id]: "0x..." }
Parameters
array
T[]
The array of objects to convert to a record
selector
K
The key used to select the property that will become the record's key
fn
(item: T) => V
The function that transforms each item in the array into the record's value
Returns
Record<T[K], V>
The resulting record object