Retrieves the value at a given keypath from an object using dot or bracket notation.
Supports keypaths like:
'foo.bar'
'foo[1].spam'
'foo["bar-baz"].quux'
"foo['bar-baz'].quux"
This function cannot retrieve values associated with symbol keys.
The object to retrieve the value from
The keypath using dot or bracket notation
Optional
Optional default value to return if the keypath is not found
The value at the keypath, or defaultValue if not found
const obj = { foo: { bar: 'hello', 'bar-baz': { quux: 'world' }, }, arr: [{ spam: 'eggs' }],};get(obj, 'foo.bar');get(obj, 'arr[0].spam');get(obj, 'foo["bar-baz"].quux');get(obj, 'foo.nonexistent');get(obj, 'foo.nonexistent', 'default'); Copy
const obj = { foo: { bar: 'hello', 'bar-baz': { quux: 'world' }, }, arr: [{ spam: 'eggs' }],};get(obj, 'foo.bar');get(obj, 'arr[0].spam');get(obj, 'foo["bar-baz"].quux');get(obj, 'foo.nonexistent');get(obj, 'foo.nonexistent', 'default');
Retrieves the value at a given keypath from an object using dot or bracket notation.
Supports keypaths like:
'foo.bar'- dot notation'foo[1].spam'- bracket notation with array indices'foo["bar-baz"].quux'- bracket notation with quoted strings"foo['bar-baz'].quux"- bracket notation with single quotesThis function cannot retrieve values associated with symbol keys.