Options
All
  • Public
  • Public/Protected
  • All
Menu

Module src/object/getArrayOfValuesFromObject

Index

Functions

Const getArrayOfValues

  • getArrayOfValues(obj: any, keysToFind: string[]): Array<any>
  • description

    Get array of values from any object, calling appropriate function depends on object type and collect values

    example

    getArrayOfValues({target: 1, key: 2}, ['target', 'key']); // return [{target: 1, key: 2}]

    getArrayOfValues([{target: 1}, {target: {a : 15}}, {target: 'test'}], ['target']); // return [{ target: 1 }, { target: {a : 15} }, { target: 'test' }]

    Parameters

    • obj: any
    • keysToFind: string[]

      array of keys to find on target object

    Returns Array<any>

Const getArrayOfValuesFromObject

  • getArrayOfValuesFromObject<T>(obj: T, keysToFind: string[], path: string | PathArray): Array<any>
  • Get values at path specified as array

    example

    getArrayOfValuesFromObject({id: 1, name: 'abc'}, ['id']); // return [{ id: 1 }]

    getArrayOfValuesFromObject({child: {id: 2}}, ['id'], 'child'); // return [{ id: 2 }]

    getArrayOfValuesFromObject({child: {id: 2}}, ['id'], 'child'); // return [{ id: 2 }]

    getArrayOfValuesFromObject( { child: { id: 1, grandChild: [ {id: 2}, {id: 3} ] } }, ['id'], 'child.grandChild' ); // return [{ id: 2 }, { id: 3 }]

    getArrayOfValuesFromObject( { arrayChild: [ { arrayGrandChild: [ { arrayGrandGrandChild:[ {id: 10, name: 'GrandGrandChildA'}, {id: 11, name: 'GrandGrandChildB'} ] } ], }, { arrayGrandChild: [ { arrayGrandGrandChild:[ {id: 10, name: 'GrandGrandChildC'}, {id: 12, name: 'GrandGrandChildA'} ] }, { arrayGrandGrandChild:[ {id: 14} ] } ], }, ], }, ['id'], 'arrayChild.arrayGrandChild.arrayGrandGrandChild' ); // return [{ id: 10 }, { id: 11 }, { id: 10 }, { id: 12 }, { id: 14 }]

    Type parameters

    • T: Record<string, any>

    Parameters

    • obj: T
    • keysToFind: string[]
    • path: string | PathArray

      ex. 'invoice.invoice_lines'

    Returns Array<any>

    {Array}

Const getArrayOfValuesFromObjectRecursively

  • getArrayOfValuesFromObjectRecursively(obj: object, keysToFind: string[], targetPath?: PathArray, currentIndex?: number): Array<any>
  • description

    Collect specific property values from object recursively

    example

    getArrayOfValuesFromObjectRecursively({id: 1, name: 'abc'}, ['id']); // return [{ id: 1 }]

    getArrayOfValuesFromObjectRecursively({child: {id: 2}}, ['child', 'id']); // return [{ id: 2 }]

    getArrayOfValuesFromObjectRecursively({childArray: [{id: 2}, {id: 3}]}, ['child', 'id']); // return [{ id: 2 }, { id: 3 }]

    getArrayOfValuesFromObjectRecursively( { child: { id: 1, grandChild: [ {id: 2}, {id: 3}, {id: 2} ] } }, ['child', 'grandChild', 'id'] ); // return [{ id: 2 }, { id: 3 }, { id: 2 }]

    getArrayOfValuesFromObjectRecursively( { arrayChild: [ { arrayGrandChild: [ { arrayGrandGrandChild:[ {id: 10}, {id: 11} ] } ], }, { arrayGrandChild: [ { arrayGrandGrandChild:[ {id: 10}, {id: 12} ] }, { arrayGrandGrandChild:[ {id: 14} ] } ], }, ], }, ['arrayChild', 'arrayGrandChild', 'arrayGrandGrandChild', 'id'] ); // return [{ id: 10 }, { id: 11 }, { id: 10 }, { id: 12 }, { id: 14 }]

    Parameters

    • obj: object

      object to be operated on

    • keysToFind: string[]

      keys to find a target path

    • Default value targetPath: PathArray = []

      use this to traverse deeper in to the object

    • Default value currentIndex: number = 0

      current index to the target path

    Returns Array<any>

Const hasReachedTargetObject

  • hasReachedTargetObject(pathToTarget: PathArray, currentIndex: number, lastIndex: number): boolean
  • Parameters

    • pathToTarget: PathArray
    • currentIndex: number
    • lastIndex: number

    Returns boolean

Generated using TypeDoc