Aberdeen - v1.0.3
    Preparing search index...

    Function multiMap

    Reactively maps items from a source proxy (array or object) to a target proxied object, where each source item can contribute multiple key-value pairs to the target.

    It iterates over the target proxy. For each item, it calls func.

    • If func returns an object, all key-value pairs from that object are added to the result proxy.
    • If func returns undefined, the item contributes nothing.

    The returned proxy automatically updates when:

    • Items are added/removed/updated in the target proxy.
    • Any proxied data read within the func call changes (for a specific item).
    • If multiple input items produce the same output key, the last one processed usually "wins", but the exact behavior on collision depends on update timing.

    This is useful for "flattening" or "indexing" data, or converting an observable array to an observable object.

    The source proxied array or object.

    A function (value, key) => ({...pairs} | undefined) that transforms an item into an object of key-value pairs to add, or undefined to add nothing.

    A new proxied object containing the aggregated key-value pairs.

    The type of items in the source proxy.

    The type of the aggregated output object (should encompass all possible key-value pairs).

    const items = proxy([
    { id: 'a', value: 10 },
    { id: 'b', value: 20 },
    ]);
    const itemsById = multiMap(items, (item) => ({
    [item.id]: item.value,
    [item.id+item.id]: item.value*10,
    }));
    // itemsById is proxy({ a: 10, aa: 100, b: 20, bb: 200 })

    $(() => console.log(itemsById));

    items.push({ id: 'c', value: 30 });
    // itemsById becomes proxy({ a: 10, aa: 100, b: 20, bb: 200, c: 30, cc: 300 })
    • When using an array as source.

      Type Parameters

      • IN
      • OUT extends { [key: string | symbol]: DatumType }

      Parameters

      • source: IN[]
      • func: (value: IN, index: number) => undefined | OUT

      Returns OUT

    • When using an object as source.

      Type Parameters

      • K extends string | number | symbol
      • IN
      • OUT extends { [key: string | symbol]: DatumType }

      Parameters

      • source: Record<K, IN>
      • func: (value: IN, index: K) => undefined | OUT

      Returns OUT