Skip to main content

latest
Works with
It is unknown whether this package works with Node.js, Deno, Browsers, Cloudflare Workers, Bun
It is unknown whether this package works with Node.js
It is unknown whether this package works with Deno
It is unknown whether this package works with Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Bun
JSR Score70%
Published2 years ago (0.215.0)
function associateWith
associateWith<T>(
array: Iterable<string>,
selector: (key: string) => T
): Record<string, T>

Builds a new Record using the given array as keys and choosing a value for each key using the given selector. If any of two pairs would have the same value the latest on will be used (overriding the ones before it).

Examples

Example 1

import { associateWith } from "@std/collections/associate_with";
import { assertEquals } from "@std/assert/assert_equals";

const names = ["Kim", "Lara", "Jonathan"];
const namesToLength = associateWith(names, (it) => it.length);

assertEquals(namesToLength, {
  "Kim": 3,
  "Lara": 4,
  "Jonathan": 8,
});

Type Parameters

Parameters

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

Return Type

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/collections

Import symbol

import { associateWith } from "@std/collections";
or

Import directly with a jsr specifier

import { associateWith } from "jsr:@std/collections";