Skip to content

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.

source

The source proxied array or object.

func

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

IN

The type of items in the source proxy.

OUT

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

Creating an index from an array

const $items = A.proxy([
{ id: 'a', value: 10 },
{ id: 'b', value: 20 },
]);
const $itemsById = A.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 })
A(() => console.log($itemsById));
$items.push({ id: 'c', value: 30 });
// $itemsById becomes proxy({ a: 10, aa: 100, b: 20, bb: 200, c: 30, cc: 300 })

multiMap<IN, OUT>(source, func): OUT

Defined in: aberdeen.ts:3355

When using an array as source.

IN

OUT extends object

IN[]

(value, index) => OUT

OUT

multiMap<K, IN, OUT>(source, func): OUT

Defined in: aberdeen.ts:3360

When using an object as source.

K extends string | number | symbol

IN

OUT extends object

Record<K, IN>

(value, index) => OUT

OUT

multiMap<K, IN, OUT>(source, func): OUT

Defined in: aberdeen.ts:3366

When using a Map as source.

K

IN

OUT extends object

Map<K, IN>

(value, key) => OUT

OUT