Skip to content

🧩 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 ​

MethodCategoryDescription
aggregateAggregationPerform complex aggregations on an array
arrangeSortingSort array by a property or function
chunkTransformationSplit array into chunks of a specific size
compactTransformationRemove null or undefined values
everyQueryCheck if all elements match a predicate
filterQueryFilter elements by predicate (supports async)
findQueryFind the first element matching a predicate
flattenTransformationFlatten nested arrays
groupAggregationGroup elements by a key or function
listPaginationClient-side reactive pagination with filtering
mapTransformationMap each element to a new value (supports async)
reduceTransformationReduce array to a single value
remoteListPaginationServer-side reactive pagination with caching
someQueryCheck if any element matches a predicate
sortSortingSort array with custom comparator
uniqSetRemove 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');

🔗 All Array Utilities ​