Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/fathym_common/src/src.deps.ts>associateBy

The Fathym Reference Architecture provides the common foundation for applications built in Typescript.
Go to Latest
function associateBy
import { associateBy } from "https://deno.land/x/fathym_common@v0.0.141/src/src.deps.ts";

Transforms the given array into a Record, extracting the key of each element using the given selector. If the selector produces the same key for multiple elements, the latest one will be used (overriding the ones before it).

Examples

Example 1

import { associateBy } from "https://deno.land/std@0.224.0/collections/associate_by.ts";
import { assertEquals } from "https://deno.land/std@0.224.0/assert/assert_equals.ts";

const users = [
  { id: "a2e", userName: "Anna" },
  { id: "5f8", userName: "Arnold" },
  { id: "d2c", userName: "Kim" },
];
const usersById = associateBy(users, (it) => it.id);

assertEquals(usersById, {
  "a2e": { id: "a2e", userName: "Anna" },
  "5f8": { id: "5f8", userName: "Arnold" },
  "d2c": { id: "d2c", userName: "Kim" },
});

Parameters

array: Iterable<T>
selector: (el: T) => string

Returns

Record<string, T>