import { isArray } from '@pnp/common/util' ; import * as jspath from 'jspath' ; import { isEmpty } from '@microsoft/sp-lodash-subset' ; export class ObjectHelper { param object param prefix public static flatten ( object , prefix = '' ) { return Object . keys ( object ) . reduce ( ( prev , element ) => { let flattenObject ; if ( element . split ( '.' ) . length > 1 ) { element = `"${ element }"` ; } if ( object [ element ] && typeof object [ element ] == 'object' && ! Array . isArray ( element ) ) { flattenObject = { ... prev , ... this . flatten ( object [ element ] , `${ prefix }${ element }.` ) } ; } else { flattenObject = { ... prev , ... { [ `${ prefix }${ element }` ] : object [ element ] } } ; } return flattenObject ; } , { } ) ; } param object param path returns public static byPath ( object , path ) { if ( path && object ) { try { const value = jspath . apply ( `.${ path }` , object ) ; if ( isEmpty ( value ) ) { return undefined ; } if ( value . toString ( ) . indexOf ( '[object Object]' ) !== - 1 ) { return JSON . stringify ( value ) ; } return value . toString ( ) ; } catch ( error ) { return object [ path ] ; } } else { return undefined ; } } }