A collection of array functions that complement JavaScript's built-in Array` methods.
▌
📦 JSR,
📦 NPM,
🌐 Web,
📰 Docs.
This package provides a comprehensive set of array functions with which you can generate an array, clone it, query about it, get non-negative indices, manage its length, get/set elements, fully or partially sort it, obtain minimum(s)/maximum(s), compare it with another array, get a part of it, search a value, obtain all possible arrangements or random arrangements, find an element, take/drop elements or scan from its beginning or its end, search the index of a part of it, perform functional operations, flatten multi-level arrays, obtain prefix sum, manipulate it in various ways, count/partition elements, split it, concatenate/join multiple arrays, rearrange elements within it, or perform set operations upon it.
Following a consistent naming convention, all functions except from*() take the array as the 1st parameter. Some functions operate on a specified range in the array and are named ranged*() (such as rangedPartialSort()). Functions like swap are pure and do not modify the array, while functions like swap$ do modify (update) the array itself — the $ suffix marks mutation. Some functions accept a map function for faster comparison (such as unique). Further, functions which return an iterable instead of an array are prefixed with i (such as isubsequences). Some names are borrowed from other languages such as Haskell, Python, Java, and Processing.
The additions cover:
- Generation: fromRange, fromInvocation, fromApplication, fromIterable.
- Cloning: shallowClone, deepClone.
- Inspection: is, keys, values, entries.
- Access: get, set, getPath, swap, remove and their mutating
$ variants.
- Ordering: sort, partialSort, isSorted, minimum, maximum, range.
- Selection: head, tail, slice, take, drop, scanWhile.
- Search: searchValue, searchInfix, searchSubsequence, indexOf, search.
- Transformation: map, filter, flat, exclusiveScan, adjacentCombine.
- Manipulation: fill, push, splice, copyWithin, rotate.
- Partitioning: count, partition, split, cutAt, chunk.
- Combination: concat, zip, interleave, cartesianProduct.
- Set operations: unique, union, intersection, difference, symmetricDifference.
import * as xarray from "jsr:@nodef/extra-array";
// 1. Get value at an index (supports negative indices).
const a = [1, 2, 3];
xarray.get(a, -1);
// → 3
// 2. Swap two values (pure, does not modify the array).
xarray.swap([1, 2, 3, 4], 0, 1);
// → [ 2, 1, 3, 4 ]
// 3. Rotate values (pure).
xarray.rotate([1, 2, 3, 4], 1);
// → [ 2, 3, 4, 1 ]
// 4. Obtain all possible arrangements.
xarray.permutations([1, 2, 3]);
// → [
// [], [ 1 ],
// [ 2 ], [ 3 ],
// [ 1, 2 ], [ 1, 3 ],
// [ 2, 1 ], [ 2, 3 ],
// [ 3, 1 ], [ 3, 2 ],
// [ 1, 2, 3 ], [ 1, 3, 2 ],
// [ 2, 1, 3 ], [ 2, 3, 1 ],
// [ 3, 1, 2 ], [ 3, 2, 1 ]
// ]
| Property |
Description |
| is |
Check if value is an array. |
| keys |
Obtain all indices. |
| values |
Get all values. |
| entries |
Obtain all index-value pairs. |
| Property |
Description |
| index |
Get zero-based index for an element in array. |
| indexRange |
Get zero-based index range for part of array. |
| Property |
Description |
| isEmpty |
Check if an array is empty. |
| length |
Find the length of an array. |
| resize$ |
Resize an array to given length! |
| clear$ |
Remove all elements from an array! |
| Property |
Description |
| get |
Get value at index. |
| getAll |
Get values at indices. |
| getPath |
Get value at path in a nested array. |
| hasPath |
Check if nested array has a path. |
| set |
Set value at index. |
| set$ |
Set value at index! |
| setPath$ |
Set value at path in a nested array! |
| swap |
Exchange two values. |
| swap$ |
Exchange two values! |
| swapRanges |
Exchange two ranges of values. |
| swapRanges$ |
Exchange two ranges of values! |
| remove |
Remove value at index. |
| remove$ |
Remove value at index! |
| removePath$ |
Remove value at path in a nested array! |
| Property |
Description |
| isEqual |
Examine if two arrays are equal. |
| compare |
Compare two arrays (lexicographically). |
| Property |
Description |
| head |
Get first value. |
| tail |
Get values except first. |
| init |
Get values except last. |
| last |
Get last value. |
| middle |
Get values from middle. |
| slice |
Get part of an array. |
| slice$ |
Get part of an array! |
| Property |
Description |
| find |
Find first value passing a test. |
| findRight |
Find last value passing a test. |
| Property |
Description |
| take |
Keep first n values only. |
| takeRight |
Keep last n values only. |
| takeWhile |
Keep values from left, while a test passes. |
| takeWhileRight |
Keep values from right, while a test passes. |
| drop |
Discard first n values only. |
| dropRight |
Discard last n values only. |
| dropWhile |
Discard values from left, while a test passes. |
| dropWhileRight |
Discard values from right, while a test passes. |
| Property |
Description |
| indexOf |
Find first index of a value. |
| lastIndexOf |
Find last index of a value. |
| search |
Find index of first value passing a test. |
| searchRight |
Find index of last value passing a test. |
| searchAll |
Find indices of values passing a test. |
| Property |
Description |
| forEach |
Call a function for each value. |
| some |
Examine if any value satisfies a test. |
| every |
Examine if all values satisfy a test. |
| map |
Transform values of an array. |
| map$ |
Transform values of an array! |
| reduce |
Reduce values of array to a single value. |
| reduceRight |
Reduce values from right, to a single value. |
| filter |
Keep values which pass a test. |
| filter$ |
Keep values which pass a test! |
| filterAt |
Keep values at given indices. |
| reject |
Discard values which pass a test. |
| reject$ |
Discard values which pass a test! |
| rejectAt |
Discard values at given indices. |
| Property |
Description |
| flat |
Flatten nested array to given depth. |
| flatMap |
Flatten nested array, based on map function. |
| Property |
Description |
| intersperse |
Place a separator between every value. |
| interpolate |
Estimate new values between existing ones. |
| intermix |
Place values of an array between another. |
| interleave |
Place values from iterables alternately. |
| zip |
Combine values from arrays. |
| Property |
Description |
| fill |
Fill with given value. |
| fill$ |
Fill with given value! |
| push |
Add value to the end. |
| push$ |
Add values to the end! |
| pop |
Remove last value. |
| pop$ |
Remove last value! |
| shift |
Remove first value. |
| shift$ |
Remove first value! |
| unshift |
Add values to the start. |
| unshift$ |
Add values to the start! |
| copy |
Copy part of array to another. |
| copy$ |
Copy part of array to another! |
| copyWithin |
Copy part of array within. |
| copyWithin$ |
Copy part of array within! |
| moveWithin |
Move part of array within. |
| moveWithin$ |
Move part of array within! |
| splice |
Remove or replace existing values. |
| splice$ |
Remove or replace existing values! |
| Property |
Description |
| count |
Count values which satisfy a test. |
| countEach |
Count occurrences of each distinct value. |
| partition |
Segregate values by test result. |
| partitionEach |
Segregate each distinct value. |
| Property |
Description |
| split |
Break array considering test as separator. |
| splitAt |
Break array considering indices as separator. |
| cut |
Break array when test passes. |
| cutRight |
Break array after test passes. |
| cutAt |
Break array at given indices. |
| cutAtRight |
Break array after given indices. |
| group |
Keep similar values together and in order. |
| chunk |
Break array into chunks of given size. |
| Property |
Description |
| concat |
Append values from arrays. |
| concat$ |
Append values from arrays! |
| join |
Join values together into a string. |
| Property |
Description |
| cycle |
Obtain values that cycle through array. |
| repeat |
Repeat an array given times. |
| reverse |
Reverse the values. |
| reverse$ |
Reverse the values! |
| rotate |
Rotate values in array. |
| rotate$ |
Rotate values in array! |
| Property |
Description |
| isUnique |
Examine if there are no duplicate values. |
| isDisjoint |
Examine if arrays have no value in common. |
| unique |
Remove duplicate values. |
| union |
Obtain values present in any array. |
| union$ |
Obtain values present in any array! |
| intersection |
Obtain values present in both arrays. |
| difference |
Obtain values not present in another array. |
| symmetricDifference |
Obtain values not present in both arrays. |
| cartesianProduct |
Obtain cartesian product of arrays. |
An array is a collection of values stored contiguously in memory. Each value has a zero-based index, and the number of values is the array's length. In JavaScript, an Array is a dynamic, resizable object whose elements may be of any type, including other arrays (hence nested arrays). Values are accessed with bracket notation (a[i]) or, for nested paths, by chaining indices (a[i][j][k]). Because JavaScript arrays are sparse-capable but typically dense, contiguous access is fast; iteration with for...of, Array.fromAsync(), or Array.from() yields values in index order.
In JavaScript, an array can be created with [], Array.of(), Array.from(), or new Array(n) (which fills with empty slots), and inspected with Array.isArray(), a.length, a.keys(), a.values(), and a.entries(). Values can be appended with a.push(), prepended with a.unshift(), removed from either end with a.pop()/a.shift(), and spliced in or out with a.splice(). Copies are made with a.slice(), and the array itself can be reordered with a.sort(), a.reverse(), or a.copyWithin(). A part of the array can be extracted with a.slice(start, end); a test applied with a.some(), a.every(), or a.find(); and a functional transformation performed with a.map(), a.filter(), and a.reduce(). These built-ins form the idiomatic baseline that this package extends.
The functions in this package mirror these built-ins where possible (get, set, slice, sort, map, filter, reduce, some, every) and add a wide range of others that are common in functional and mathematical code: partial sorting (as in NumPy's partition), prefix scans (inclusive and exclusive), range-based variants (ranged*()), iterable-returning variants (i*()), random selection (random*()), and set operations (union, intersection, difference, symmetricDifference, cartesianProduct). The $ suffix consistently denotes in-place mutation, so a call to xarray.map$(a, fn) is exactly analogous to Array.prototype.map but writes into the original array; the pure form xarray.map(a, fn) returns a new array. This separation makes it easy to opt in to mutation and to reason about which calls allocate.
Because the package is a single flat module, every function is reachable from the same import. Groupings in the Index above are by purpose, not by submodule, so you can scan for the operation you want and then confirm the exact signature in the @nodef/extra-array` docs.

