Array Methods
chunk
Chunks an array into multiple arrays, each containing size or fewer items.
Since: 0.1.0
Arguments
Param | Type | Description |
---|---|---|
array | any[] | The array to process. |
size | number | The length of each chunk |
Returns
Type | Description |
---|---|
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
Param | Type | Description |
---|---|---|
array | any[] | The array to inspect. |
other | ...any[] | The arrays to exclude. |
[transformer] | Function | The transformer invoked per element for comparison. |
Returns
Type | Description |
---|---|
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
Param | Type | Description |
---|---|---|
array | ...any[] | The array to inspect. |
[transformer] | Function | The transformer invoked per element for comparison. |
Returns
Type | Description |
---|---|
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
Param | Type | Description |
---|---|---|
start | number | The start value of array, if arguments.length === 0 , array started at 0 value |
size | number | The size of array |
Returns
Type | Description |
---|---|
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
Param | Type | Description |
---|---|---|
array | any[] | The array to query |
Returns
Type | Description |
---|---|
any[] | Returns the new shuffled array. |
Declaration
declare const shuffle: <T>(array: T[]) => T[];