import { keyBy } from "https://deno.land/x/ayonli_jsext@v0.9.72/array/index.ts";
Creates a record or map from the items of the array according to the comparable values returned by a provided callback function.
This function is similar to groupBy, except it overrides values if the same property already exists instead of grouping them as a list.
Examples
Example 1
Example 1
import { keyBy } from "@ayonli/jsext/array";
const arr = [
{ id: 1, name: "foo" },
{ id: 2, name: "bar" },
{ id: 3, name: "baz" },
];
console.log(keyBy(arr, item => item.name));
// {
// foo: { id: 1, name: "foo" },
// bar: { id: 2, name: "bar" },
// baz: { id: 3, name: "baz" }
// }
Examples
Example 1
Example 1
import { keyBy } from "@ayonli/jsext/array";
const arr = [
{ id: 1, name: "foo" },
{ id: 2, name: "bar" },
{ id: 3, name: "baz" },
];
console.log(keyBy(arr, item => item.name, Map));
// Map {
// "foo" => { id: 1, name: "foo" },
// "bar" => { id: 2, name: "bar" },
// "baz" => { id: 3, name: "baz" }
// }