Aberdeen - v1.0.3
    Preparing search index...

    Function onEach

    Reactively iterates over the items of an observable array or object, optionally rendering content for each item.

    Automatically updates when items are added, removed, or modified.

    The observable array or object to iterate over. Values that are undefined are skipped.

    A function called for each item in the array. It receives the item's (observable) value and its index/key. Any DOM elements created within this function will be associated with the item, placed at the right spot in the DOM, and cleaned up when redrawing/removing the item.

    An optional function to generate a sort key for each item. This controls the order in which items are rendered in the DOM. If omitted, items are rendered in array index order. The returned key can be a number, string, or an array of numbers/strings for composite sorting. Use invertString on string keys for descending order. Returning null or undefined from makeKey will prevent the item from being rendered.

    const items = proxy(['apple', 'banana', 'cherry']);

    // Basic iteration
    onEach(items, (item, index) => $(`li:${item} (#${index})`));

    // Add a new item - the list updates automatically
    setTimeout(() => items.push('durian'), 2000);
    // Same for updates and deletes
    setTimeout(() => items[1] = 'berry', 4000);
    setTimeout(() => delete items[2], 6000);
    const users = proxy([
    { id: 3, group: 1, name: 'Charlie' },
    { id: 1, group: 1, name: 'Alice' },
    { id: 2, group: 2, name: 'Bob' },
    ]);

    // Sort by name alphabetically
    onEach(users, (user) => {
    $(`p:${user.name} (id=${user.id})`);
    }, (user) => [user.group, user.name]); // Sort by group, and within each group sort by name
    const config = proxy({ theme: 'dark', fontSize: 14, showTips: true });

    // Display configuration options
    $('dl', () => {
    onEach(config, (value, key) => {
    if (key === 'showTips') return; // Don't render this one
    $('dt:'+key);
    $('dd:'+value);
    });
    });

    // Change a value - the display updates automatically
    setTimeout(() => config.fontSize = 16, 2000);

    invertString To easily create keys for reverse sorting.

    • Type Parameters

      • T

      Parameters

      • target: (undefined | T)[]
      • render: (value: T, index: number) => void
      • OptionalmakeKey: (value: T, key: any) => SortKeyType

      Returns void

    • Type Parameters

      • K extends string | number | symbol
      • T

      Parameters

      • target: Record<K, undefined | T>
      • render: (value: T, index: K) => void
      • OptionalmakeKey: (value: T, key: K) => SortKeyType

      Returns void