Function associateWith
- associate
With <T>(array, selector): Record<string, T> Type Parameters
Parameters
Returns Record<string, T>
An object where each element of the array is associated with a value returned by the selector function.
Example: Basic usage
import { associateWith } from "@std/collections/associate-with";
import { assertEquals } from "@std/assert";
const names = ["Kim", "Lara", "Jonathan"];
const namesToLength = associateWith(names, (person) => person.length);
assertEquals(namesToLength, {
"Kim": 3,
"Lara": 4,
"Jonathan": 8,
});
Associates each string element of an array with a value returned by a selector function.
If any of two pairs would have the same value, the latest one will be used (overriding the ones before it).