Skip to content

Modules: Object

Alex Bruno Cáceres edited this page Mar 2, 2023 · 1 revision

Useful methods to deal with objects.

getPropertyValue

Gets the value of a deep property path in a given object or returns a default value if it doesn't exist.

If a default value is not provided, undefined is returned by default.

This is exactly the same as lodash.get(object, path, value) but this is made with cleaner, smarter, modern vanilla JS native code, and no bloat imports like the lodash method.

import { getPropertyValue } from '@jsweb/utils/modules/object'

const obj = {
  a: {
    b: {
      c: {
        d: 0,
        e: 'test',
      },
    },
  },
}
const t1 = getPropertyValue(obj, 'a.b.c.d') // 0
const t2 = getPropertyValue(obj, 'a.b.c.e') // test
const t3 = getPropertyValue(obj, 'a.b.c.d.e') // undefined
const t4 = getPropertyValue(obj, 'a.b.c.d.e', 'none') // none
const t5 = getPropertyValue(obj, ['a', 'b', 'c', 'e']) // test

Clone this wiki locally