Skip to main content

Array Methods

chunk

Chunks an array into multiple arrays, each containing size or fewer items.

Since: 0.1.0

Arguments

ParamTypeDescription
arrayany[]The array to process.
sizenumberThe length of each chunk

Returns

TypeDescription
Array<any[]>Returns new array of chunks

Declaration

declare const chunk: <T>(array: T[], size: number) => T[][];

Examples

const array = [1, 2, 3, 4, 5, 6];
chunk(array, 2); // [[1, 2], [3, 4], [5, 6]]

difference

Creates an array of array values not included in the other given arrays. The order and references of result values are determined by the first array.

Since: 0.1.0

Arguments

ParamTypeDescription
arrayany[]The array to inspect.
other...any[]The arrays to exclude.
[transformer]FunctionThe transformer invoked per element for comparison.

Returns

TypeDescription
any[]Returns the new array of filtered values.

Declaration

declare function difference<T>(
array: T[],
...other: T[][] | [...T[][], (value: T, index: number) => any]
): T[];

Examples

const arr = [1, 2, 3, 4, 5];
const other = [3, 5, 6];
difference(arr, other); // [1, 2, 4]
difference(arr, other, [2], (v) => v % 5); // [2, 4]

intersection

Creates an array of values that are included in all given arrays. The order and references of result values are determined by the first array.

Since: 0.1.0

Arguments

ParamTypeDescription
array...any[]The array to inspect.
[transformer]FunctionThe transformer invoked per element for comparison.

Returns

TypeDescription
any[]Returns the new array of intersecting values.

Declaration

declare function intersection<T>(
...arrays: T[][] | [...T[][], (value: T, index: number) => any]
): T[];

Examples

const arr = [1, 2, 3, 4, 5];
const other = [3, 5, 6];
intersection(arr, other); // [3, 5]
intersection(arr, other, [5], (value) => value > 3); // [4, 5, 6]

range

A function to create numbered lists of integers,

Since: 0.1.0

Arguments

ParamTypeDescription
startnumberThe start value of array, if arguments.length === 0, array started at 0 value
sizenumberThe size of array

Returns

TypeDescription
number[]Returns size length array, started at start

Declaration

declare function range(size: number): number[];
declare function range(start: number, size: number): number[];

Examples

range(5); // [0, 1, 2, 3, 4]
range(3, 4); // [3, 4, 5, 6]

shuffle

Creates an array of shuffled values, using knuth shuffle

Since: 0.1.0

Arguments

ParamTypeDescription
arrayany[]The array to query

Returns

TypeDescription
any[]Returns the new shuffled array.

Declaration

declare const shuffle: <T>(array: T[]) => T[];