Home Docs Modules Array Array Utilities Pure, non-mutating array manipulation functions in sc-fns/array.
The sc-fns/array module provides pure, non-mutating array helpers for chunking, shuffling, deduplication, grouping, and sampling.
ts
import {
shuffleArray,
randomItem,
randomItems,
chunkArray,
uniqueArray,
uniqueBy,
flattenArray,
groupBy,
removeItem,
moveItem,
} from "sc-fns/array" ;
Randomly shuffles an array using the Fisher-Yates algorithm. Returns a new array without mutating the original input.
ts
function shuffleArray < T >( array : readonly T []) : T [];
ts
const items = [ 1 , 2 , 3 , 4 , 5 ];
const shuffled = shuffleArray (items);
console. log (shuffled); // e.g. [3, 1, 5, 2, 4]
console. log (items); // [1, 2, 3, 4, 5] (Unmodified)
Splits an array into smaller sub-arrays (chunks) of a specified maximum size.
ts
function chunkArray < T >( array : readonly T [], size : number ) : T [][];
ts
chunkArray ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 ], 3 );
// [[1, 2, 3], [4, 5, 6], [7]]
Deduplicates an array of primitive values while preserving order.
ts
function uniqueArray < T >( array : readonly T []) : T [];
ts
uniqueArray ([ 1 , 2 , 2 , 3 , 4 , 4 , 1 ]);
// [1, 2, 3, 4]
uniqueArray ([ "apple" , "banana" , "apple" ]);
// ["apple", "banana"]
Deduplicates an array of objects based on a property key or an iteratee function.
ts
function uniqueBy < T , K >( array : readonly T [], iteratee : keyof T | (( item : T ) => K )) : T [];
ts
const users = [
{ id: 1 , name: "Alice" },
{ id: 2 , name: "Bob" },
{ id: 1 , name: "Alice Duplicate" },
];
uniqueBy (users, "id" );
// [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }]
uniqueBy (users, ( u ) => u.name. toLowerCase ());
Picks one or multiple random items from an array.
ts
function randomItem < T >( array : readonly T []) : T | undefined ;
function randomItems < T >(
array : readonly T [],
count : number ,
options ?: { replacement ?: boolean },
) : T [];
ts
const colors = [ "red" , "green" , "blue" , "yellow" ];
randomItem (colors);
// "blue"
randomItems (colors, 2 );
// ["green", "red"]
Groups elements of an array by a property key or custom discriminator function into an object dictionary.
ts
function groupBy < T , K extends PropertyKey >(
array : readonly T [],
iteratee : keyof T | (( item : T ) => K ),
) : Record < K , T []>;
ts
const products = [
{ name: "Laptop" , category: "tech" },
{ name: "Coffee" , category: "food" },
{ name: "Phone" , category: "tech" },
];
groupBy (products, "category" );
// {
// tech: [{ name: "Laptop", ... }, { name: "Phone", ... }],
// food: [{ name: "Coffee", ... }]
// }
Flattens nested arrays up to a specified depth (defaults to 1).
ts
function flattenArray < T >( array : readonly any [], depth ?: number ) : T [];
ts
flattenArray ([ 1 , [ 2 , [ 3 , 4 ]]], 1 ); // [1, 2, [3, 4]]
flattenArray ([ 1 , [ 2 , [ 3 , 4 ]]], 2 ); // [1, 2, 3, 4]
Non-mutating utilities to remove or reorder items.
ts
// Remove an item by value or predicate
removeItem ([ 10 , 20 , 30 , 40 ], 20 ); // [10, 30, 40]
removeItem (users, ( u ) => u.id === 2 );
// Move item from index 0 to index 2
moveItem ([ "a" , "b" , "c" , "d" ], 0 , 2 );
// ["b", "c", "a", "d"]
Last updated on October 25, 2026