Returns an array split into chunks. If the array can't be split equally based on the given size, the last chunk will be the remaining elements.
import 'ts-arrays'
const arr = [1, 2, 3, 4, 5]
const chunkedArr = arr.chunk(2)
// => [[1, 2], [3, 4], 5]
Size of each chunk
Returns the common values from all the given arrays.
import 'ts-arrays'
const arr1 = [1, 2, 3, 4]
const arr2 = [2, 'cat']
const arr3 = [1.1, 2.2, 3]
const arr4 = [[ 'cheetah', 'rhino' ], 4]
const commonArr = arr1.common(arr2, arr3, arr4)
// => [2, 3, 4]
Checks if all the elements in the source array are present in the other arrays (args).
import 'ts-arrays'
const arr1 = [1, 2, 3, 4]
const arr2 = [2, 'cat']
const arr3 = [1.1, 2.2, 3]
const arr4 = [[ 'cheetah', 'rhino' ], 4]
const arr5 = ['deno', 'land']
const arr6 = ['land', 'deno']
const falseyArr = arr1.containsAll(arr1, arr2, arr3, arr4)
// => false
const truthyArr = arr5.containsAll(arr6)
// => true
Returns the unique elements in an array compared to the rest of the arrays (args).
import 'ts-arrays'
const arr1 = [1, 2, 3, 4]
const arr2 = [2, 'cat']
const arr3 = [1.1, 2.2, 3]
const arr4 = [[ 'cheetah', 'rhino' ], 4]
const diffArr = arr1.diff(arr2, arr3, arr4)
// => [1]
Flattens an array and checks if all the elements are of the specified type.
import 'ts-arrays'
const arr = [['dog', ['sun', 'sky'], 'moon'], 'test']
const isValid = arr.isType('number')
// => false
Type to match
Merge the elements from the rest of the arrays (args) to the first array. A nested array will be considered as a single value.
import 'ts-arrays'
const arr1 = [1, 2, 3, 4]
const arr2 = ['dog', 'cat']
const arr3 = [1.1, 2.2, 3.3]
const arr4 = [[ 'cheetah', 'rhino' ], 'monkey']
const mergedArr = arr1.merge(arr2, arr3, arr4)
// => [1, 2, 3, 4, 'dog', 'cat', 1.1, 2.2, 3.3, ['cheetah', 'rhino'], 'monkey']
Rest of the arrays
Returns an array without the values passed as args.
import 'ts-arrays'
const arr = [1, 1, 'Dog', 'Dog', 123.42, 123.42]
const res = arr.remove('Dog')
// => [1, 1, 123.42, 123.42]
const res2 = arr.remove(1, 'Dog')
// => [123.42, 123.42]
Elements to remove
Converts the nested arrays of an array into a single object of key-value pairs.
import 'ts-arrays'
const arr = [['name', 'deno'], ['location', 'land']].toObject()
// => { 'name': 'deno', 'location': 'land' }
Returns the unique values from a given array. Optionally pass in a boolean to specify if the result should be sorted.
import 'ts-arrays'
const arr = [1, 1, 'Dog', 'Dog', 123.42, 123.42]
const uniqueArr = arr.unique()
// => [1, 'Dog', 123.42]
Return sorted values, defaults to false
Generated using TypeDoc
Extends Array.Prototype with the below functions.