Create a new `Map` instance from an array.
const array = [ { id: 1, name: "Fred" }, { id: 2, name: "Annie" },]; mapify( array, (item) => item.id, (item) => item.name,);// Map(2) { 1 => 'Fred', 2 => 'Annie' }
npx atmx add helper mapify
Copy and paste the following method into @/utils/helpers/undefined.ts:
@/utils/helpers/undefined.ts
/*** Create a new `Map` instance from an array.** @example* const array = [* { id: 1, name: 'Fred' },* { id: 2, name: 'Annie' },* ]** mapify(* array,* item => item.id,* item => item.name,* )* // Map(2) { 1 => 'Fred', 2 => 'Annie' }*/export function mapify<T, Key, Value = T>(array: readonly T[],getKey: (item: T, index: number) => Key,getValue: (item: T, index: number) => Value = (item) =>item as unknown as Value,): Map<Key, Value> {const map: Map<Key, Value> = new Map();for (const item of array) {map.set(getKey(item, map.size), getValue(item, map.size));}return map;}