🧩 Array Utilities ​
Array utilities provide a powerful set of tools to transform, query, and manipulate arrays in a type-safe, ergonomic way. Use these helpers to write cleaner, more expressive code for common array operations like mapping, filtering, grouping, and more.
📚 Quick Reference ​
| Method | Category | Description |
|---|---|---|
aggregate | Aggregation | Perform complex aggregations on an array |
arrange | Sorting | Sort array by a property or function |
chunk | Transformation | Split array into chunks of a specific size |
compact | Transformation | Remove null or undefined values |
every | Query | Check if all elements match a predicate |
filter | Query | Filter elements by predicate (supports async) |
find | Query | Find the first element matching a predicate |
flatten | Transformation | Flatten nested arrays |
group | Aggregation | Group elements by a key or function |
list | Pagination | Client-side reactive pagination with filtering |
map | Transformation | Map each element to a new value (supports async) |
reduce | Transformation | Reduce array to a single value |
remoteList | Pagination | Server-side reactive pagination with caching |
some | Query | Check if any element matches a predicate |
sort | Sorting | Sort array with custom comparator |
uniq | Set | Remove duplicate values |
💡 Practical Examples ​
Data Transformation ​
ts
import { map, chunk, compact, uniq } from '@vielzeug/toolkit';
const rawData = [1, 2, null, 2, 3, undefined, 4];
// 1. Clean data (remove nulls/undefined)
const clean = compact(rawData); // [1, 2, 2, 3, 4]
// 2. Get unique values
const unique = uniq(clean); // [1, 2, 3, 4]
// 3. Transform values
const doubled = map(unique, (x) => x * 2); // [2, 4, 6, 8]
// 4. Batch for processing
const batches = chunk(doubled, 2); // [[2, 4], [6, 8]]Advanced Grouping & Sorting ​
ts
import { group, arrange } from '@vielzeug/toolkit';
const users = [
{ name: 'Alice', role: 'admin', age: 30 },
{ name: 'Bob', role: 'user', age: 25 },
{ name: 'Charlie', role: 'user', age: 35 },
];
// Group by role
const byRole = group(users, (u) => u.role);
/*
{
admin: [{ name: 'Alice', ... }],
user: [{ name: 'Bob', ... }, { name: 'Charlie', ... }]
}
*/
// Sort by age (descending)
const oldestFirst = arrange(users, (u) => u.age, 'desc');