Options
All
  • Public
  • Public/Protected
  • All
Menu

Namespace Arrays

Extends Array.Prototype with the below functions.

Index

Functions

chunk

  • chunk(array: Array<any>, size: number): Array<any>
  • 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]

    Parameters

    • array: Array<any>
    • size: number

      Size of each chunk

    Returns Array<any>

common

  • 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]

    Parameters

    • array: Array<any>
    • Rest ...args: Array<any>

      Rest of the arrays

    Returns Array<any>

compact

  • Returns an array with all falsey values (false, null, 0, '', undefined, and NaN) removed.

    import 'ts-arrays'
    
    const arr = [1, 2, 3, 4, NaN, 5, null, 6]
    const compactArr = arr.compact()
    // => [1, 2, 3, 4, 5, 6]

    Parameters

    Returns Array<any>

containsAll

  • containsAll(source: Array<any>, ...args: Array<any>): Boolean
  • 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

    Parameters

    • source: Array<any>
    • Rest ...args: Array<any>

      Rest of the arrays

    Returns Boolean

diff

  • 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]

    Parameters

    • array: Array<any>
    • Rest ...args: Array<any>

      Rest of the arrays

    Returns Array<any>

flatten

  • Flattens an arrays of arrays into a single array.

    import 'ts-arrays'
    
    const arr = [[ 'cheetah', 'rhino', ['sun', 'moon'], [['nested nested', 'test']]], 4]
    const res = arr.flatten()
    // => ['cheetah', 'rhino', 'sun', 'moon', 'nested nested', 'test', 4]

    Parameters

    Returns Array<any>

isType

  • isType(array: Array<any>, type: string): Boolean
  • 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

    Parameters

    • array: Array<any>
    • type: string

      Type to match

    Returns Boolean

Const isValidArray

  • isValidArray(array: Array<any>): Boolean
  • Parameters

    Returns Boolean

merge

  • merge(array: Array<any>, ...args: any): Array<any>
  • 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']

    Parameters

    • array: Array<any>
    • Rest ...args: any

      Rest of the arrays

    Returns Array<any>

remove

  • remove(array: Array<any>, ...args: any): Array<any>
  • 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]

    Parameters

    • array: Array<any>
    • Rest ...args: any

      Elements to remove

    Returns Array<any>

toObject

  • toObject(array: Array<any>): Object
  • 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' }

    Parameters

    Returns Object

unique

  • unique(array: Array<any>, sort?: Boolean): Array<any>
  • 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]

    Parameters

    • array: Array<any>
    • Default value sort: Boolean = false

      Return sorted values, defaults to false

    Returns Array<any>

Generated using TypeDoc