Reactively partitions items from a source proxy (array or object) into multiple "bucket" proxies
based on keys determined by a classifier function.
This function iterates through the source proxy using onEach. For each item,
it calls the classifier func, which should return:
A single key (OUT_K): The item belongs to the bucket with this key.
An array of keys (OUT_K[]): The item belongs to all buckets specified in the array.
undefined: The item is not placed in any bucket.
The function returns a main proxied object. The keys of this object are the bucket keys (OUT_K)
returned by func. Each value associated with a bucket key is another proxied object (the "bucket").
This inner bucket object maps the original keys/indices from the source to the items
themselves that were classified into that bucket.
The entire structure is reactive. Changes in the source proxy (adding/removing/updating items)
or changes in dependencies read by the func will cause the output partitioning to update automatically.
Buckets are created dynamically as needed and removed when they become empty.
Param: source
The input proxied Array or Record (e.g., created by proxy) containing the items to partition.
Param: func
A classifier function (value: IN_V, key: IN_K | number) => undefined | OUT_K | OUT_K[].
It receives the item's value and its original key/index from the source. It returns the bucket key(s)
the item belongs to, or undefined to ignore the item.
Returns
A proxied object where keys are the bucket identifiers (OUT_K) and values are proxied Records
(Record<IN_K | number, IN_V>) representing the buckets. Each bucket maps original source keys/indices
to the items belonging to that bucket.
Template: OUT_K
The type of the keys used for the output buckets (string, number, or symbol).
Template: IN_V
The type of the values in the source proxy.
Template: IN_K
The type of the keys in the source proxy (if it's a Record).
// Partition products by category. Output keys are categories (string). // Inner keys are original array indices (number). constproductsByCategory = partition(products, (product) =>product.category);
// Reactively show the data structure dump(productsByCategory);
// Make random changes to the categories, to show reactiveness setInterval(() =>products[0|(Math.random()*3)].category = ['Snack','Fruit','Veg'][0|(Math.random()*3)], 2000);
Reactively partitions items from a source proxy (array or object) into multiple "bucket" proxies based on keys determined by a classifier function.
This function iterates through the
source
proxy using onEach. For each item, it calls the classifierfunc
, which should return:OUT_K
): The item belongs to the bucket with this key.OUT_K[]
): The item belongs to all buckets specified in the array.undefined
: The item is not placed in any bucket.The function returns a main proxied object. The keys of this object are the bucket keys (
OUT_K
) returned byfunc
. Each value associated with a bucket key is another proxied object (the "bucket"). This inner bucket object maps the original keys/indices from thesource
to the items themselves that were classified into that bucket.The entire structure is reactive. Changes in the
source
proxy (adding/removing/updating items) or changes in dependencies read by thefunc
will cause the output partitioning to update automatically. Buckets are created dynamically as needed and removed when they become empty.Param: source
The input proxied Array or Record (e.g., created by proxy) containing the items to partition.
Param: func
A classifier function
(value: IN_V, key: IN_K | number) => undefined | OUT_K | OUT_K[]
. It receives the item's value and its original key/index from thesource
. It returns the bucket key(s) the item belongs to, orundefined
to ignore the item.Returns
A proxied object where keys are the bucket identifiers (
OUT_K
) and values are proxied Records (Record<IN_K | number, IN_V>
) representing the buckets. Each bucket maps original source keys/indices to the items belonging to that bucket.Template: OUT_K
The type of the keys used for the output buckets (string, number, or symbol).
Template: IN_V
The type of the values in the source proxy.
Template: IN_K
The type of the keys in the source proxy (if it's a Record).
Example: Grouping items by a property
Example: Item in multiple buckets