MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / indexBy

Method indexBy

packages/core/src/entity/Collection.ts:840–848  ·  view source on GitHub ↗

* Maps the collection items to a dictionary, indexed by the key you specify. * If there are more items with the same key, only the first one will be present.

(
    key: K1,
    valueKey?: K2,
  )

Source from the content-addressed store, hash-verified

838 * If there are more items with the same key, only the first one will be present.
839 */
840 indexBy<K1 extends keyof T, K2 extends keyof T = never>(
841 key: K1,
842 valueKey?: K2,
843 ): Record<T[K1] & PropertyKey, T> | Record<T[K1] & PropertyKey, T[K2]> {
844 return this.reduce((obj, item) => {
845 obj[item[key] as string] ??= valueKey ? item[valueKey] : item;
846 return obj;
847 }, {} as any);
848 }
849
850 /** Returns whether the collection has been initialized. Pass `fully = true` to also check that all items are initialized. */
851 isInitialized(fully = false): boolean {

Callers 1

getConfigurationMethod · 0.80

Calls 1

reduceMethod · 0.95

Tested by

no test coverage detected