partition
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.
source
The input proxied Array or Record (e.g., created by proxy) containing the items to partition.
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.
Template
Section titled “Template”OUT_K
The type of the keys used for the output buckets (string, number, or symbol).
Template
Section titled “Template”IN_V
The type of the values in the source proxy.
Template
Section titled “Template”IN_K
The type of the keys in the source proxy (if it’s a Record).
Examples
Section titled “Examples”Grouping items by a property
interface Product { id: string; category: string; name: string; }const $products = A.proxy<Product[]>([ { id: 'p1', category: 'Fruit', name: 'Apple' }, { id: 'p2', category: 'Veg', name: 'Carrot' }, { id: 'p3', category: 'Fruit', name: 'Banana' },]);
// Partition products by category. Output keys are categories (string).// Inner keys are original array indices (number).const $productsByCategory = A.partition($products, ($product) => $product.category);
// Reactively show the data structureA.dump($productsByCategory);
// Make random changes to the categories, to show reactivenesssetInterval(() => $products[0|(Math.random()*3)].category = ['Snack','Fruit','Veg'][0|(Math.random()*3)], 2000);Item in multiple buckets
interface User { id: number; tags: string[]; name: string; }const $users = A.proxy({ 'u1': { name: 'Alice', tags: ['active', 'new'] }, 'u2': { name: 'Bob', tags: ['active'] }});
// Partition users by tag. Output keys are tags (string).// Inner keys are original object keys (string: 'u1', 'u2').const $usersByTag = A.partition($users, ($user) => $user.tags);
console.log($usersByTag);Call Signature
Section titled “Call Signature”partition<
OUT_K,IN_V>(source,func):Record<OUT_K,Record<number,IN_V>>
Defined in: aberdeen.ts:3430
When using an object as array.
Type Parameters
Section titled “Type Parameters”OUT_K extends string | number | symbol
IN_V
Parameters
Section titled “Parameters”source
Section titled “source”IN_V[]
(value, key) => OUT_K | OUT_K[]
Returns
Section titled “Returns”Record<OUT_K, Record<number, IN_V>>
Call Signature
Section titled “Call Signature”partition<
IN_K,OUT_K,IN_V>(source,func):Record<OUT_K,Record<IN_K,IN_V>>
Defined in: aberdeen.ts:3435
When using an object as source.
Type Parameters
Section titled “Type Parameters”IN_K extends string | number | symbol
OUT_K extends string | number | symbol
IN_V
Parameters
Section titled “Parameters”source
Section titled “source”Record<IN_K, IN_V>
(value, key) => OUT_K | OUT_K[]
Returns
Section titled “Returns”Record<OUT_K, Record<IN_K, IN_V>>
Call Signature
Section titled “Call Signature”partition<
IN_K,OUT_K,IN_V>(source,func):Record<OUT_K,Record<IN_K,IN_V>>
Defined in: aberdeen.ts:3444
When using a Map as source.
Type Parameters
Section titled “Type Parameters”IN_K extends string | number | symbol
OUT_K extends string | number | symbol
IN_V
Parameters
Section titled “Parameters”source
Section titled “source”Map<IN_K, IN_V>
(value, key) => OUT_K | OUT_K[]
Returns
Section titled “Returns”Record<OUT_K, Record<IN_K, IN_V>>