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'constobj={a: {b: {c: {d: 0,e: 'test',},},},}constt1=getPropertyValue(obj,'a.b.c.d')// 0constt2=getPropertyValue(obj,'a.b.c.e')// testconstt3=getPropertyValue(obj,'a.b.c.d.e')// undefinedconstt4=getPropertyValue(obj,'a.b.c.d.e','none')// noneconstt5=getPropertyValue(obj,['a','b','c','e'])// test

Clone this wiki locally