Aberdeen - v1.0.3
    Preparing search index...

    Function map

    Reactively maps/filters items from a proxied source array or object to a new proxied array or object.

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

    • If func returns a value, it's added to the result proxy under the same key/index.
    • If func returns undefined, the item is skipped (filtered out).

    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).

    A function (value, key) => mappedValue | undefined that transforms each item. It receives the item's value and its key/index. Return undefined to filter the item out.

    A new proxied array or object containing the mapped values.

    The type of items in the source proxy.

    The type of items in the resulting proxy.

    const numbers = proxy([1, 2, 3]);
    const doubled = map(numbers, (n) => n * 2);
    // doubled is proxy([2, 4, 6])

    observe(() => console.log(doubled)); // Logs updates
    numbers.push(4); // doubled becomes proxy([2, 4, 6, 8])
    const users = proxy({
    'u1': { name: 'Alice', active: true },
    'u2': { name: 'Bob', active: false },
    'u3': { name: 'Charlie', active: true }
    });

    const activeUserNames = map(users, (user) => user.active ? user.name : undefined);
    // activeUserNames is proxy({ u1: 'Alice', u3: 'Charlie' })
    observe(() => console.log(Object.values(activeUserNames)));

    users.u2.active = true;
    // activeUserNames becomes proxy({ u1: 'Alice', u2: 'Bob', u3: 'Charlie' })
    • When using an object as source.

      Type Parameters

      • IN
      • OUT

      Parameters

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

      Returns Record<string | symbol, OUT>

    • When using an array as source.

      Type Parameters

      • IN
      • OUT

      Parameters

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

      Returns OUT[]